-2

This is my Select

SELECT id, name, price, category, url, image1 FROM `Products_items` WHERE active='y' and stockCount >= 1 ORDER BY views DESC

But I need to know column category_url from table Product_categories.




I tried this one but doesnt work

SELECT id, name, price, category, url, image1 FROM `Products_items` WHERE active='y' and stockCount >= 1 ORDER BY views DESC
SELECT url FROM Product_categories WHERE id=Products_items.category
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 2
    Hint: `JOIN` connects two tables. – Gordon Linoff Apr 25 '17 at 14:07
  • How do you relation products and product categories? – Troyer Apr 25 '17 at 14:08
  • 2
    Possible duplicate of [How can I join multiple SQL tables using the IDs?](http://stackoverflow.com/questions/9853586/how-can-i-join-multiple-sql-tables-using-the-ids) – Shadow Apr 25 '17 at 14:11
  • @Troyer product_items.category is Product_categories.id – Lucas Ferraz Apr 25 '17 at 14:16
  • It is absolutely unfair to edit your question and add some follow-up questions that were not in scope of the original one. If you have any follow-up questions, then ask them separately. Although, there are dozens of questions here on SO alone that deal with how to print out results of a query from php... – Shadow Apr 25 '17 at 14:48

2 Answers2

0

You can use a JOIN to join two tables, you have more information there. In case you have a foreign key on the products_items table related to the category you can do this:

SELECT a.id, a.name, a.price, a.category, b.url, a.image1 
FROM `Products_items` a 
LEFT JOIN Product b ON a.category_id = b.id 
WHERE a.active='y' and a.stockCount >= 1 ORDER BY a.views DESC
Shadow
  • 33,525
  • 10
  • 51
  • 64
Troyer
  • 6,765
  • 3
  • 34
  • 62
0

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

SELECT * FROM Products_items as items JOIN Product_categories as categories ON items.category = categories.id;
Vasili
  • 895
  • 7
  • 12