我需要使用表/结果集/具有整数n到m的任何内容进行连接.有没有一种简单的方法来获得它而不仅仅是建立表格?
(顺便说一句,那种类型的构造会被称为"元查询"?)
mn受限于某种合理的(<1000)
我在网上找到了这个解决方案
SET @row := 0; SELECT @row := @row + 1 as row, t.* FROM some_table t, (SELECT @row := 0) r
单个查询,快速,并且完全符合我的要求:现在我可以"编号"从复杂查询中找到的"选择",其中唯一数字从1开始,并为结果中的每一行递增一次.
我认为这也适用于上面列出的问题:调整初始起始值@row
并添加限制子句以设置最大值.
顺便说一句:我认为"r"并不是真正需要的.
DDSP
以下将返回1..10000并且不会那么慢
SELECT @row := @row + 1 AS row FROM (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t, (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2, (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3, (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4, (SELECT @row:=0) numbers;
如果您碰巧使用MySQL的MariaDB分支,则SEQUENCE
引擎允许直接生成数字序列.它通过使用虚拟(假)一列表来实现.
例如,要生成从1到1000的整数序列,请执行此操作
SELECT seq FROM seq_1_to_1000;
对于0到11,执行此操作.
SELECT seq FROM seq_0_to_11;
对于从今天开始的一周的连续DATE值,请执行此操作.
SELECT FROM_DAYS(seq + TO_DAYS(CURDATE)) dateseq FROM seq_0_to_6
DATE
从'2010-01-01'开始,连续十年的价值是这样做的.
SELECT FROM_DAYS(seq + TO_DAYS('2010-01-01')) dateseq FROM seq_0_to_3800 WHERE FROM_DAYS(seq + TO_DAYS('2010-01-01')) < '2010-01-01' + INTERVAL 10 YEAR
如果您没有使用MariaDB,请考虑一下.
MySQL中没有序列号生成器(CREATE SEQUENCE).最近的是AUTO_INCREMENT
,它可以帮助你构建表.
你可以尝试这样的事情:
SELECT @rn:=@rn+1 as n FROM (select @rn:=2)t, `order` rows_1, `order` rows_2 --, rows_n as needed... LIMIT 4
其中order
只是一些具有相当大的行集的表的示例.
编辑:原来的答案是错误的,任何功劳应归功于David Poor,他提供了相同概念的实例
1到100.000之间的数字序列:
SELECT e*10000+d*1000+c*100+b*10+a n FROM (select 0 a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t1, (select 0 b union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2, (select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3, (select 0 d union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4, (select 0 e union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t5 order by 1
我用它来检查某些数字是否不正确,例如:
select * from ( select 121 id union all select 123 union all select 125 union all select 126 union all select 127 union all select 128 union all select 129 ) a right join ( SELECT e*10000+d*1000+c*100+b*10+a n FROM (select 0 a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t1, (select 0 b union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2, (select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3, (select 0 d union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4, (select 0 e union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t5 order by 1 ) seq on seq.n=a.id where seq.n between 121 and 129 and id is null
结果将是121和129之间的序列数122和124的间隔:
id n ---- --- null 122 null 124
也许它可以帮助某人!