开发者

Join two tables who both have a record

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?

Max


An 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜