SELECT collections.title, collections.uniqueID, uploads.uniqueID as thumb
FROM collections
INNER JOIN uploads ON collections.uniqueID = uploads.uploadGroup
WHERE collections.owner = @ownerin this instance if I have 3 rows in "uploads" that have the matching uniqueid/uploadgroup (as in 3 uploads in a collection) this gives me back 3 rows.
what I'm looking for is ONE row for each DISTINCT COLLECTIONS.UNIQUEID - is this possible?
so when collections has
1 | title | idhereand uploads has
1 | uniqueID1 | idhere
2 | uniqueID2 | idhere
3 | uniqueID3 | idhereI'm currently returning
1 | title | uniqueID1
2 | title | uniqueID2
3 | title | uniqueID3when all I want to return is
1 | title | uniqueID1 2 3 Answers
If you need a simple distinct list from the uploads table then you can do it like this:
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 Here's one approach (sql server only):
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 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 0