2017-04-25 42 views
-2

这是我的选择如何在此选择内做选择? MySQL的

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

但我需要从表Product_categories知道列category_url




我想这一个,但不工作

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 
+2

提示:'JOIN'连接两个表。 –

+0

您如何关联产品和产品类别? – Troyer

+2

[我如何可以使用ID连接多个SQL表?](http://stackoverflow.com/questions/9853586/how-can-i-join-multiple-sql-tables-using-the-ids) – Shadow

回答

0

您可以使用JOIN来连接两个表,你有更多的信息there。如果您有相关的,你可以做到这一点类别的products_items表的外键:

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 
+0

我编辑的主要帖子,我怎么能在PHP的变种呢? –

+0

@LucasFerraz立即检查。 – Troyer

+0

你好!非常感谢! –

0

一个JOIN子句用于行从两个或多个表结合起来,根据它们之间的相关列。

SELECT * FROM Products_items as items JOIN Product_categories as categories ON items.category = categories.id; 
+0

我编辑了主帖,如何在php中获得vars? –