Table "images" with sample data:
id | user_id | album_id | sort
1 15 1 1
2 15 1 2
3 15 1 0
4 15 2 0
5 15 2 1
6 15 3 0
MySQL Query:
SELECT id, sort, COUNT(*) FROM images WHERE user_id=15 GROUP BY album_id
The query returns one row per unique combination. However, how can you tell which row's id
it chooses? E.g. the image with sort=0
is the cover image of an album. The file path to this image contains the id
. So, how is it possible to always get the id of the row, where sort=0
?
I've tried MIN(sort)
, which returns always 0
in the sort
column, however not the right id
for that column...? ORDER BY sort
only sorts the results... not a solution either.
Any help is highly appreciated! Thank you very much in advance!
EDIT: hope this will help for better understanding:
The query e.g. gives the following results:
id | sort | count
1 1 3
4 0 2
6 0 1
BUT, how is it possible to get always this specific row, where the value of sort
is 0
? E.g.
id | sort | count
3 0 3
4 0 2
6 0 1