简体   繁体   中英

Reading a database diagram

I've been trying to figure out how to get the products that match a certain category id but I have been unable to figure out how to move from category to products.

How would a query that basically selects all products that match a certain category id look?

在此处输入图片说明

This should work:

SELECT products.*
FROM products,
     product_category
WHERE product_category.categoryid = CATEGORY_ID
  AND products.catalogid = product_category.catalogid

Or if you prefer a join:

SELECT products.*
FROM products
INNER JOIN product_category ON products.catalogid = product_category.catalogid
WHERE product_category.categoryid = CATEGORY_ID

Simply replace CATEGORY_ID by the ID of the category you wish to select.

product_category is a link table, joining the tables products and product_category together: it contains the catalogid , referencing the ID of the category, and catalogid , referencing the ID of the product.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM