Let's have this sample data:
+-------+---------+
| col1 | col2 |
+-------+---------+
| 111 | hello |
| 222 | hello |
| 111 | blabla |
| 444 | there |
| 555 | blabla |
| 555 | there |
+-------+---------+
I need a SQL returning distinct values for each columns separately (as feed for dropdown values for filtering). Thus the result should be:
+-------+---------+
| col1 | col2 |
+-------+---------+
| 111 | hello |
| 222 | blabla |
| 444 | there |
| 555 | |
+-------+---------+
The results need not be in this format; it's more important that I have the distinct values for easy access and iteration.
The closest I got is from here: https://stackoverflow.com/a/12188117/169252
select (SELECT group_concat(DISTINCT col1) FROM testtable) as col1, (SELECT group_concat(DISTINCT col2) FROM testtable) as col2;
This returns:
+-----------------+-------------------+
| col1 | col2 |
+-----------------+-------------------+
| 111,222,444,555 | velo,hallo,blabla |
+-----------------+-------------------+
That's pretty close, and I'll choose this one if no better solution comes up; it's not optimal as values are comma separated and I need to split the results.
I also tried:
SELECT DISTINCT from col1,col2 FROM testtable;
This returns the distint values of BOTH columns, not what I want.
Also:
select col1,col2 from testtable group by col1,col2;
which has been suggested elsewhere doesn't return what I need, but returns each column in-distinct :)