如何在 mysql 5.X in sql 的结果集中创建 3 的倍数作为输出
How to create multiples of 3 in the result set as output in mysql 5.X in sql
在结果集中,我应该得到类似于整数 * 乘以 3 的值,到一定的限制,比如 ex 直到 20,即。直到 20*3 =60
在 mysql 5.X 中仅使用 Sql
col1
3
6
9
12
15
..
..
..
..
在oracle中我们可以通过使用level子句轻松做到。
在 MySQL 中,您可以创建一个过程以将结果存储在临时 table 中并在之后显示它,如下所示:
DELIMITER $
CREATE PROCEDURE `temp`(upto integer)
BEGIN
DROP TABLE IF EXISTS multiple;
CREATE TABLE multiple(col1 integer);
SET @i := 1;
while @i <= upto DO
set @val := 3 * @i;
set @sql := CONCAT('INSERT INTO multiple values(',@val,')');
prepare b from @sql;
execute b;
set @i := @i + 1;
end while;
select * from multiple;
end $
DELIMITER ;
call temp(20);
我从 post 中找到了这个答案并对其进行了修改以满足此要求。
select SeqValue*3 from (SELECT
(HUNDREDS.SeqValue + TENS.SeqValue + ONES.SeqValue) SeqValue
FROM
(
SELECT 0 SeqValue
UNION ALL
SELECT 1 SeqValue
UNION ALL
SELECT 2 SeqValue
UNION ALL
SELECT 3 SeqValue
UNION ALL
SELECT 4 SeqValue
UNION ALL
SELECT 5 SeqValue
UNION ALL
SELECT 6 SeqValue
UNION ALL
SELECT 7 SeqValue
UNION ALL
SELECT 8 SeqValue
UNION ALL
SELECT 9 SeqValue
) ONES
CROSS JOIN
(
SELECT 0 SeqValue
UNION ALL
SELECT 10 SeqValue
UNION ALL
SELECT 20 SeqValue
UNION ALL
SELECT 30 SeqValue
UNION ALL
SELECT 40 SeqValue
UNION ALL
SELECT 50 SeqValue
UNION ALL
SELECT 60 SeqValue
UNION ALL
SELECT 70 SeqValue
UNION ALL
SELECT 80 SeqValue
UNION ALL
SELECT 90 SeqValue
) TENS
CROSS JOIN
(
SELECT 0 SeqValue
UNION ALL
SELECT 100 SeqValue
UNION ALL
SELECT 200 SeqValue
UNION ALL
SELECT 300 SeqValue
UNION ALL
SELECT 400 SeqValue
UNION ALL
SELECT 500 SeqValue
UNION ALL
SELECT 600 SeqValue
UNION ALL
SELECT 700 SeqValue
UNION ALL
SELECT 800 SeqValue
UNION ALL
SELECT 900 SeqValue
) HUNDREDS) as a where seqValue != 0 limit 20;
CONNECT BY LEVEL
是 Oracle 用于递归查询的旧的专有语法。
SQL 标准改为使用递归 CTE,从 11.2 版开始由 Oracle 支持,从 8 版开始由 MySQL 支持。
with recursive numbers(number) as
(
select 3
union all
select number + 3 from numbers where number + 3 <= 20
)
select number
from numbers
order by number;
演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b497c18f4632d4a7fe77a6a0027a08e6
在结果集中,我应该得到类似于整数 * 乘以 3 的值,到一定的限制,比如 ex 直到 20,即。直到 20*3 =60 在 mysql 5.X 中仅使用 Sql
col1
3
6
9
12
15
..
..
..
..
在oracle中我们可以通过使用level子句轻松做到。
在 MySQL 中,您可以创建一个过程以将结果存储在临时 table 中并在之后显示它,如下所示:
DELIMITER $
CREATE PROCEDURE `temp`(upto integer)
BEGIN
DROP TABLE IF EXISTS multiple;
CREATE TABLE multiple(col1 integer);
SET @i := 1;
while @i <= upto DO
set @val := 3 * @i;
set @sql := CONCAT('INSERT INTO multiple values(',@val,')');
prepare b from @sql;
execute b;
set @i := @i + 1;
end while;
select * from multiple;
end $
DELIMITER ;
call temp(20);
我从 post 中找到了这个答案并对其进行了修改以满足此要求。
select SeqValue*3 from (SELECT
(HUNDREDS.SeqValue + TENS.SeqValue + ONES.SeqValue) SeqValue
FROM
(
SELECT 0 SeqValue
UNION ALL
SELECT 1 SeqValue
UNION ALL
SELECT 2 SeqValue
UNION ALL
SELECT 3 SeqValue
UNION ALL
SELECT 4 SeqValue
UNION ALL
SELECT 5 SeqValue
UNION ALL
SELECT 6 SeqValue
UNION ALL
SELECT 7 SeqValue
UNION ALL
SELECT 8 SeqValue
UNION ALL
SELECT 9 SeqValue
) ONES
CROSS JOIN
(
SELECT 0 SeqValue
UNION ALL
SELECT 10 SeqValue
UNION ALL
SELECT 20 SeqValue
UNION ALL
SELECT 30 SeqValue
UNION ALL
SELECT 40 SeqValue
UNION ALL
SELECT 50 SeqValue
UNION ALL
SELECT 60 SeqValue
UNION ALL
SELECT 70 SeqValue
UNION ALL
SELECT 80 SeqValue
UNION ALL
SELECT 90 SeqValue
) TENS
CROSS JOIN
(
SELECT 0 SeqValue
UNION ALL
SELECT 100 SeqValue
UNION ALL
SELECT 200 SeqValue
UNION ALL
SELECT 300 SeqValue
UNION ALL
SELECT 400 SeqValue
UNION ALL
SELECT 500 SeqValue
UNION ALL
SELECT 600 SeqValue
UNION ALL
SELECT 700 SeqValue
UNION ALL
SELECT 800 SeqValue
UNION ALL
SELECT 900 SeqValue
) HUNDREDS) as a where seqValue != 0 limit 20;
CONNECT BY LEVEL
是 Oracle 用于递归查询的旧的专有语法。
SQL 标准改为使用递归 CTE,从 11.2 版开始由 Oracle 支持,从 8 版开始由 MySQL 支持。
with recursive numbers(number) as
(
select 3
union all
select number + 3 from numbers where number + 3 <= 20
)
select number
from numbers
order by number;
演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b497c18f4632d4a7fe77a6a0027a08e6