备注:测试数据库版本为MySQL 8.0
代码
-- 用with递归构造1-9的数据 with RECURSIVE c(n) as (select 1 n union all select n + 1 from c where n < 9), tmp1 as ( SELECT c1.n n1,c2.n n2,CONCAT(c1.n,' * ',c2.n,' = ',c1.n*c2.n) exp from c c1 inner join c c2 on c1.n <= c2.n ) select max(case when n1 = 1 then exp else null end) as A1, max(case when n1 = 2 then exp else null end) as A2, max(case when n1 = 3 then exp else null end) as A3, max(case when n1 = 4 then exp else null end) as A4, max(case when n1 = 5 then exp else null end) as A5, max(case when n1 = 6 then exp else null end) as A6, max(case when n1 = 7 then exp else null end) as A7, max(case when n1 = 8 then exp else null end) as A8, max(case when n1 = 9 then exp else null end) as A9 from tmp1 group by n2 order by n2
复制
测试记录
mysql> -- 用with递归构造1-9的数据 mysql> with RECURSIVE c(n) as -> (select 1 n union all select n + 1 from c where n < 9), -> tmp1 as -> ( -> SELECT c1.n n1,c2.n n2,CONCAT(c1.n,' * ',c2.n,' = ',c1.n*c2.n) exp -> from c c1 -> inner join c c2 -> on c1.n <= c2.n -> ) -> select max(case when n1 = 1 then exp else null end) as A1, -> max(case when n1 = 2 then exp else null end) as A2, -> max(case when n1 = 3 then exp else null end) as A3, -> max(case when n1 = 4 then exp else null end) as A4, -> max(case when n1 = 5 then exp else null end) as A5, -> max(case when n1 = 6 then exp else null end) as A6, -> max(case when n1 = 7 then exp else null end) as A7, -> max(case when n1 = 8 then exp else null end) as A8, -> max(case when n1 = 9 then exp else null end) as A9 -> from tmp1 -> group by n2 -> order by n2; +-----------+------------+------------+------------+------------+------------+------------+------------+------------+ | A1 | A2 | A3 | A4 | A5 | A6 | A7 | A8 | A9 | +-----------+------------+------------+------------+------------+------------+------------+------------+------------+ | 1 * 1 = 1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | | 1 * 2 = 2 | 2 * 2 = 4 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | | 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 | NULL | NULL | NULL | NULL | NULL | NULL | | 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 | NULL | NULL | NULL | NULL | NULL | | 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 | NULL | NULL | NULL | NULL | | 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 | NULL | NULL | NULL | | 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 | NULL | NULL | | 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 | NULL | | 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 | +-----------+------------+------------+------------+------------+------------+------------+------------+------------+ 9 rows in set (0.00 sec)
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
墨天轮个人数说知识点合集
JiekeXu
442次阅读
2025-04-01 15:56:03
MySQL数据库当前和历史事务分析
听见风的声音
426次阅读
2025-04-01 08:47:17
MySQL 生产实践-Update 二级索引导致的性能问题排查
chengang
388次阅读
2025-03-28 16:28:31
【MySQL 30周年庆】MySQL 8.0 OCP考试限时免费!教你免费领考券
墨天轮小教习
385次阅读
2025-04-25 18:53:11
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
严少安
375次阅读
2025-04-25 15:30:58
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
357次阅读
2025-04-17 17:02:24
MySQL 9.3 正式 GA,我却大失所望,新特性亮点与隐忧并存?
JiekeXu
354次阅读
2025-04-15 23:49:58
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
331次阅读
2025-04-15 14:48:05
云和恩墨杨明翰:安全生产系列之MySQL高危操作
墨天轮编辑部
308次阅读
2025-03-27 16:45:26
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
294次阅读
2025-04-07 12:14:29