查询时遇到一些麻烦,我希望你们都可以帮我看看我失踪了什么。 COLLECTIONS中有三行,相册的值为1。但无论出于何种原因,查询只返回1行而不是3行。
任何想法?
SELECT a.id assetID
, a.uri
, a.caption
, u.alias
, u.icon
, u.link
, u.id userID
FROM collections c
JOIN assets a
ON a.id = c.asset
JOIN users u
ON u.id = a.user
WHERE c.album = 1
ORDER
BY a.ts DESC
继承数据结构
集合
album asset
1 1
2 2
1 3
1 4
2 5
资产
id uri user private new ts type caption
1 1-1391997727-15835000.jpg 1 public 0 1368847153 image this is a test file thingy
2 2-1391997760-42134600.jpg 1 public 0 1368847353 image more test files WOOHOO 0 0
3 3-1391997811-96846600.jpg 3 public 0 1368847553 image i took this picture wooo
4 3-1391997840-97233100.jpg 3 public 0 1368848232 image
5 3-1391997859-93753900.jpg 3 public 0 1368848781 image w00t
USERS
id alias link icon
1 dbunting NULL NULL
答案 0 :(得分:1)
您在结果集中获得了一行,因为用户ID为1的表资产中存在一个匹配的行。
这是联接工作方式的一个很好的链接:What is the difference between Left, Right, Outer and Inner Joins?
此查询:
SELECT
c.album,
a.id assetID
, a.uri
, a.caption
, u.alias
, u.icon
, u.link
, u.id userID
FROM collections c
INNER JOIN assets a ON (a.id = c.asset AND c.album=1)
LEFT JOIN users u ON u.id = a.user
这将输出专辑1的3行
播放