常用SQL:修订间差异
跳到导航
跳到搜索
(→生成连续数字) |
|||
第1行: | 第1行: | ||
== MySQL == | == MySQL == | ||
==== 生成连续数字 ==== | ==== 生成连续数字 ==== | ||
* MySQL | |||
with recursive seq(no) as( | with recursive seq(no) as( | ||
select 1 no | select 1 no | ||
第10行: | 第11行: | ||
select no | select no | ||
from seq | from seq | ||
* MySQL, Doris, Oracle | |||
with t1 as ( | |||
with a as ( | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 | |||
), | |||
b as ( | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 | |||
) | |||
select 1 from a, b | |||
), | |||
t2 as ( | |||
with a as ( | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 | |||
), | |||
b as ( | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 | |||
) | |||
select 1 from a, b | |||
), | |||
t3 as ( | |||
with a as ( | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 | |||
), | |||
b as ( | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL | |||
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 | |||
) | |||
select 1 from a, b | |||
limit 100 | |||
) | |||
select 1 from t1, t2, t3 | |||
==== 固定长度数字 ==== | ==== 固定长度数字 ==== | ||
substr(concat('00000', floor(rand() * 1000000)), -6) | substr(concat('00000', floor(rand() * 1000000)), -6) |
2023年12月5日 (二) 15:55的版本
MySQL
生成连续数字
- MySQL
with recursive seq(no) as( select 1 no union all select no+1 no from seq where no < 100 ) select no from seq
- MySQL, Doris, Oracle
with t1 as ( with a as ( SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ), b as ( SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) select 1 from a, b ), t2 as ( with a as ( SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ), b as ( SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) select 1 from a, b ), t3 as ( with a as ( SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ), b as ( SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) select 1 from a, b limit 100 ) select 1 from t1, t2, t3
固定长度数字
substr(concat('00000', floor(rand() * 1000000)), -6)
近一百天内时间
- Doris: date_add(now(), - rand() * 100)
- MySQL: date_add(now(), interval - RAND() * 100 day)