MySQL 多联合快捷方式

MySQL Multi Union Shortcut

我想要一个 mysql table 来垂直列出数据而不使用 table 值。

就这样。

SELECT 0 AS vertical UNION SELECT 1 UNION SELECT 2 UNION SELECT 3;

这将输出

vertical
0
1
2
3

现在,有没有办法用更少的代码来做到这一点?如果我想列出最多 50 个?

使用递归 CTE 生成具有或不具有任何范围间隙的数字范围。

没有间隙:

-- MySQL (v8.0)
-- range between 0 to 50 with no gap
WITH RECURSIVE num_range (n) AS
(
  SELECT 0 -- starting the range
  UNION ALL
  SELECT n + 1 -- increment by 1
  FROM num_range 
  WHERE n < 50 -- last number
)
SELECT n
FROM num_range;

有差距:

-- range between 0 to 50 with gap
WITH RECURSIVE num_range (n) AS
(
  SELECT 0 -- starting the range
  UNION ALL
  SELECT n + 2  -- increment by 2
  FROM num_range 
  WHERE n < 50 -- last number
)
SELECT n
FROM num_range;

请检查这个urlhttps://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=30e714fbb43b0d2bf55974bea50410f9