开发者

How to select rows which are evenly distributed on some column?

开发者 https://www.devze.com 2023-03-10 07:59 出处:网络
I have a table tbl with 3 columns imp_col, data1, data2 where imp_col\'s type is enum(\'imp1\', \'imp2, \'imp3\', 开发者_Python百科\'imp4\'). Now if I want to select 10 rows randomly I can do like thi

I have a table tbl with 3 columns imp_col, data1, data2 where imp_col's type is enum('imp1', 'imp2, 'imp3', 开发者_Python百科'imp4'). Now if I want to select 10 rows randomly I can do like this.

select imp_col, data1, data2 from tbl where imp_col in ('imp1', 'imp2', 'imp3') limit 10 order by rand();
But in this case all the 10 resulting rows may have imp_col as imp1.

Is there an SQL query which can return 10 rows with imp_col distributed evenly among imp1, imp2, imp3?

Thanks.


Are you able to use limit and rand() in an inline view in mySQL?

      select Y.c1, Y.c2, Y.c3
      from
      (
      select c1, c2, c3.... from X limit 500 order by rand()
      ) as Y
      where Y.c2 in ( 'imp1', 'imp2', 'imp3')
      limit 10

That would give you a better chance of getting a variety of 'imp' values, if it's legal.


You should try with

imp_col LIKE "imp1"
OR imp_col LIKE "imp2"
OR imp_col LIKE "imp3"

I think IN doesn't works for enums

0

精彩评论

暂无评论...
验证码 换一张
取 消