开发者

PHP SQL - Joining three tables

开发者 https://www.devze.com 2023-02-18 05:22 出处:网络
I have three tables called pr开发者_如何学JAVAoduct, product_category and category with product_category being the linking table.

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}";
0

精彩评论

暂无评论...
验证码 换一张
取 消