0

Products と ProductImages の 2 つのテーブルがあり、Products と ProductImages の間に 1 対多の関係があります。ProductImages テーブルのレコードが一致する行のみが結果に含まれるという条件で、Products テーブルに対してクエリを実行しようとしています。

Products
----------
id (PK)

ProductImages
---------------
id (PK)
product_id (FK to Products)

私ができる唯一の方法はサブクエリを使用することですが、より良い/より効率的な方法があるはずです。

4

4 に答える 4

2
SELECT p.* FROM Products AS p
INNER JOIN ProductImages AS pi ON p.id = pi.product_id
GROUP BY p.id
于 2012-06-28T06:34:11.893 に答える
2

ユーザーjoin

SELECT * FROM Products INNER JOIN ProductImage  on 
Products.id  = ProductImage.product_id 
于 2012-06-28T06:39:08.827 に答える
0
select * from products
where id in (select product_id from ProductImages)
于 2012-06-28T06:37:17.133 に答える
0
SELECT Products.* FROM Products
INNER JOIN ProductImages ON Products.id = ProductImages.id
and Products.Id = @ProductID // if required this condition
于 2012-06-28T06:39:31.730 に答える