Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL INNER JOIN with DISTINCT rows?

SELECT collections.title, collections.uniqueID, uploads.uniqueID as thumb 
FROM collections 
INNER JOIN uploads ON collections.uniqueID = uploads.uploadGroup 
WHERE collections.owner = @owner

in 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 | idhere

and uploads has

1 | uniqueID1 | idhere
2 | uniqueID2 | idhere
3 | uniqueID3 | idhere

I'm currently returning

1 | title | uniqueID1
2 | title | uniqueID2
3 | title | uniqueID3

when all I want to return is

1 | title | uniqueID1
like image 244
korben Avatar asked Jun 04 '26 22:06

korben


1 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
like image 68
Vince Pergolizzi Avatar answered Jun 07 '26 13:06

Vince Pergolizzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!