简体   繁体   中英

Mysql Select one row for each value of a colum

I have a table like this :

ID - CatID - Value
1      1      foo
2      2      bar
3      2      barbar
4      1      foobar
5      1      barfoo
6      2      foofoo

And what I need is to select randomly one row of each CatId (in this example it should 2 rows randomly). I tried with sub-query and Join on itself but I'm not sure it's as nice as it cool be... So if you have an idea on how to do it nicely it would be nice to help.

Thanks !

slow, but works:
select * from (select * from Tbl order by rand()) as t1 group by CatID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM