SELECT collections.title, collections.uniqueID, uploads.uniqueID as thumb
FROM collections
INNER JOIN uploads ON collections.uniqueID = uploads.uploadGroup
WHERE collections.owner = @owner
在这种情况下,如果我在“uploads”中有3行具有匹配的uniqueid / uploadgroup(如在集合中的3个上传中),这会让我回到3行。
我正在寻找的是每个DISTINCT COLLECTIONS.UNIQUEID的一行 - 这可能吗?
所以当集合有
时1 | title | idhere
和上传有
1 | uniqueID1 | idhere
2 | uniqueID2 | idhere
3 | uniqueID3 | idhere
我正在回来
1 | title | uniqueID1
2 | title | uniqueID2
3 | title | uniqueID3
当我想要归还的时候是
1 | title | uniqueID1
答案 0 :(得分:4)
如果您需要上传表格中的简单不同列表,那么您可以这样做:
SELECT collections.title, collections.uniqueID, uploads.uniqueID as thumb
FROM collections
INNER JOIN (SELECT DISTINCT uniqueID FROM uploads) uploads ON collections.uniqueID = uploads.uploadGroup
WHERE collections.owner = @owner
答案 1 :(得分:3)
这是一种方法(仅限sql server):
SELECT collections.title, collections.uniqueID, uploads.uniqueID as thumb
FROM collections
CROSS APPLY (
SELECT TOP 1 *
FROM uploads
WHERE collections.uniqueID = uploads.uploadGroup
ORDER BY uploads.uniqueID -- not required, can be changed to any column you want
) uploads
答案 2 :(得分:2)
SELECT collections.title, collections.uniqueID, min(uploads.uniqueID) as thumb
FROM collections
JOIN uploads ON collections.uniqueID = uploads.uploadGroup
WHERE collections.owner = @owner
GROUP BY collections.title, collections.uniqueID