问题描述
嗨,这个查询,我试图在 MysqL 中运行,以便在我的 ID 上获得良好的分布,但看起来语法有问题。
select min(ID),max(ID),count(*),nt from ( select ID,ntile(16) over (order by ID) nt from table) group by nt order by nt;
这在 Oracle 中有效,但在 MysqL 中无效,可能看起来在 MysqL 5.7 中不可用。 我们还能如何获得这些数据?
基本上我生成了 UUID 应用程序,它可以排序,需要组织和分组,然后分成 16 段。
预期输出
MIN(ID) MAX(ID) COUNT(*) NT
00000000-ebc5-4d19-9d7b 0a360b83-6d9a-17d7-9b67 36282227 1
0a360b83-6d9a-17d7-9b67 0a360b85-6ebb-1bbc-9bbb 36282227 2
解决方法
NTILE for MYSQL 5,7 和 Mariadb 10.1
** 和之前的版本**它需要一些逻辑,如果你愿意,你可以调试它
第一个是我的应用,第二个是你的查询的mysql 80版本比较
我仍然建议升级您的 mysql 版本
主要部分是
@mod:=countr % 16,@div:=countr DIV 16
在哪里确定您需要的瓷砖数量
SELECT
MIN(ID),MAX(ID),COUNT(*),nt
FROM
(SELECT
`ID`,IF(@countr < @div2,@ntile,@ntile:=@ntile + 1) AS nt,@countr:=@countr + 1,@countr:=1) c1,IF(@ntile <= CAST(@mod AS UNSIGNED),@div2:=@div + 1,@div2:=@div) div2
FROM
(SELECT
ID,@mod:=countr % 16,@div:=countr DIV 16,@div2:=@div
FROM
table1,(SELECT
COUNT(*) countr
FROM
table1,(SELECT @ntile:=1,@countr:=0,@div2:=0) t3) t2) t1
ORDER BY ID) t1
GROUP BY nt
ORDER BY CAST(nt AS UNSIGNED);
select
min(ID),max(ID),count(*),nt
from
( select
ID,ntile(16) over (order by ID) nt
from table1) t1
group by nt order by nt;
dbfiddle here