I need to join two tables with a shared ID, but I only want to return a row if both tables contain a row. I found a solution with sub-queries, but want to avoid them (because this query is run multiple times a page load).
An example:
`Products`:
Name PicID
------|-------
Test1 | 4
Test2 | 5
开发者_高级运维
`Images`:
PicID Picture
------|--------
4 | BLOB
A query would only return Test1 (with the blob) because Test2 doesn't have a row in the picture table.
Thoughts?
MaxAn INNER JOIN
is used to guarantee that the matching values exist in both tables.
SELECT p.PicID, p.Name, i.Picture
FROM Products p
INNER JOIN Images i
ON p.PicID = i.PicID
SELECT P.Name, P.PicID, I.Picture
FROM Products P
INNER JOIN Images I
ON p.PicID = I.PicID
Select *
FROM Products, Images
WHERE Product.PicID = Images.PicID
精彩评论