Reputation: 2042
I've got two tables, Products and ProductImages with a one to many relationship between Products and ProductImages. I'm trying to work a query on on the Products table with a condition that the results contain only rows with matching records in the ProductImages table.
Products
----------
id (PK)
ProductImages
---------------
id (PK)
product_id (FK to Products)
The only way I can work it is with a subquery, but surely there must be a better/more efficient way.
Upvotes: 0
Views: 1066
Reputation:
user join
SELECT * FROM Products INNER JOIN ProductImage on
Products.id = ProductImage.product_id
Upvotes: 2
Reputation: 2677
SELECT Products.* FROM Products
INNER JOIN ProductImages ON Products.id = ProductImages.id
and Products.Id = @ProductID // if required this condition
Upvotes: 0
Reputation: 204766
select * from products
where id in (select product_id from ProductImages)
Upvotes: 0
Reputation: 4062
SELECT p.* FROM Products AS p
INNER JOIN ProductImages AS pi ON p.id = pi.product_id
GROUP BY p.id
Upvotes: 2