-3

データベースに3つのテーブルがあると仮定します

Table:Category   
   category_id

Table:container   
   container_id
   category_id

Table:products

   container_id
   product_id

category_id に基づいてすべての product_id を取得するにはどうすればよいですか?

たとえば、上記のテーブル内に次のデータがあります。

Table: category
    sour
    sweet
    Bitter


Table: container
    bottled
    sachet

Table: product
    sugar
    vinegar
    cocoa

tb_productカテゴリ ( tb_category) が甘いすべての製品 ( ) を取得するにはどうすればよいですか

4

3 に答える 3

1

INNER JOIN問題を解決するために使用できます。

SELECT  a.*
FROM    products a
        INNER JOIN container b
            on a.container_id = b.container_ID
        INNER JOIN Category c
            ON b.category_ID = c.categoryID
WHERE   c.categoryName = 'sweete'

ご覧のとおり、Categoryテーブルには と の列があるcategory_IDと仮定しますcategoryName。上記の例では、カテゴリ名を使用して、特定のカテゴリに属する​​すべての製品を検索しました。

于 2012-09-09T13:37:06.140 に答える
0
select p.*
from Category c
join container c2 on c2.category_id = c.category_id
join product p on p.container_id = c2.container_id
where c.name = 'sweet';

クエリ内のテーブルの順序に注意してください (他の回答よりも高速に実行されるはずです!)

于 2012-09-09T13:38:39.070 に答える
0

次のように内部結合を使用できます。

SELECT a.product_id FROM products AS a INNER JOIN  container AS b on a.container_id = b.container_Id INNER JOIN Category AS c on  b.category_Id = c.category_id where c.category_id = 'new'
于 2012-09-09T13:39:38.167 に答える