I have three tables called pr开发者_如何学JAVAoduct, product_category and category with product_category being the linking table.
How can I join these tables using SQL in PHP?
The linking table has only productID linking to the product table and catID linking to the category.
Something like this?
SELECT
*
FROM
product
INNER JOIN
product_category
ON product_category.productID = product.productID
INNER JOIN
category
ON category.catID = product_category.catID
Your query should look something like this: Added the requirement that there should be a productId and categoryId from a variable:
$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}";
精彩评论