MySQL returns fewer rows with GROUP BY statement
Posted
by bschaeffer
on Stack Overflow
See other posts from Stack Overflow
or by bschaeffer
Published on 2010-03-14T04:04:38Z
Indexed on
2010/03/14
4:15 UTC
Read the original article
Hit count: 160
mysql
I've got a MySQL database that stores image information. Right now it only has three rows stored in the database, and each row is associated with something like, for instance, a unique blog post via a key column.
As of right now, one "blog post key" has one image, and one has two images in the database.
When I run this query, MySQL returns all three rows.
SELECT `id`, `key`, `url`
FROM (`images`)
WHERE `key` = 'TpaS4G5h'
OR `key` = '78855e44'
However, when i add the GROUP BY
statement I only get two rows... one for each key.
SELECT `id`, `key`, `url`
FROM (`images`)
WHERE `key` = 'TpaS4G5h'
OR `key` = '78855e44'
GROUP BY `key`
I'm sure there is a simple solution, but I don't know what it is... so any help would be really appreciated.
Thanks in advance!
© Stack Overflow or respective owner