我有一个包含100000行的表,在其中有一个列,比如drugname,其类型是varchar。
列的值可以从0到9开始,a或a开始。
我需要一个返回25000行的查询。在这25000行中,它应该包含所有的字母和数字。
比如说,
这包括所有的数字和字母表,总的来说,它应该有25000行。
发布于 2016-04-05 13:52:28
首先为所有组选择最小的ids集(按第一个字符)
select min(id) as substrId
from the_table
group by substr(value,0,1)然后使用子选择首先返回选定的ids (表示所有组),然后返回一些随机ids。
select t1.* 
from the_table t1
    left join (select min(id) as substrId
               from the_table
               group by substr(value,0,1)) sub on t1.id=sub.substrId
order by ifnull(sub.substrId, 0) desc
limit 25000因此,所有不同的组成员都包含在最后的结果集中。
https://stackoverflow.com/questions/36427642
复制相似问题