我有三个名为product,product_category和category的表,product_category是链接表。
如何在PHP中使用SQL连接这些表?
链接表只有productID链接到product表和catID链接到类别。
答案 0 :(得分:4)
这样的东西?
SELECT
*
FROM
product
INNER JOIN
product_category
ON product_category.productID = product.productID
INNER JOIN
category
ON category.catID = product_category.catID
答案 1 :(得分:2)
您的查询应如下所示: 添加了变量中应该有productId和categoryId的要求:
$query = "SELECT * FROM
product p
JOIN product_category pc ON p.id = pc.productId
JOIN category c ON c.id = pc. categoryId
WHERE p.id = {$productId}
AND c.id = {$categoryId}";