Let’s say I have the following MySQL table:
id | species ------------ 1 | dog 2 | dog 3 | dog 4 | cat 5 | cat 6 | fish 7 | fish 8 | fish
I want to get the ids of 2 dogs, 1 cat, and 1 fish (they don’t have to be in order). For example, {1, 2, 4, 6} would be a valid output.
Is there a way to do this in one SQL query? As far as I know, LIMIT is a fairly simple operator that can’t do this. Maybe WHERE can be manipulated? Or would multiple SQL queries be the best way of doing this?
As far as I know the best you can do is to use
UNIONto combine the results of threeSELECTs, e.g.: