我有两张桌子:
'people'
id | name | which_image
--------------------------------
1 | Joey | 1
2 | Bobby | 2
3 | Jimmy | 3
'images'
id | filename
--------------------------------
1 | joe_face.jpg
2 | bob_angry.jpg
3 | jim_laughs.jpg
我如何选择在几个人的“人员”表中获取所有列,并在同一个查询中获取他们的文件名?我试过这个,但它返回一个空集:
SELECT p.*, i.filename FROM people p, images i
WHERE ( p.id = 1 OR p.id = 3 )
AND p.which_image = i.id
答案 0 :(得分:0)
试试这个:
SELECT p.*,
i.filename
FROM people p
INNER JOIN images i
ON p.which_image = i.id
WHERE p.id = 1
OR p.id = 3;
答案 1 :(得分:0)
由于“图像”中没有真正的外键,因此请使用以下子选项:
SELECT p.*, (select filename from images i where i.id=p.which_image) as filename
FROM people p
WHERE p.id in (1,3)