首页 > 数据库 >MySQL8.0新特性CTE表达式递归实现累加运算 1+2+…+n 等于多少?

MySQL8.0新特性CTE表达式递归实现累加运算 1+2+…+n 等于多少?

时间:2024-05-25 23:05:09浏览次数:15  
标签:last sum t1 current CTE sumtest num MySQL8.0 累加

上一篇内容,通过MySQL存储过程实现累加运算 1+2+…+n 等于多少的需求,使用当前主流版本MySQL5.7.x 和MySQL8.0.x,以及最新的MySQL8.4 LST版本。

WITH AS子句在MySQL 8.0.x及更高版本中得到支持,而在MySQL 5.7及以下版本中则不支持。

参考地址如下:
https://blog.csdn.net/zxrhhm/article/details/139185014?spm=1001.2014.3001.5501

1、本文内容

MySQL8.0新特性CTE(Common Table Expressions)的子句表达式,递归实现累加运算 1+2+…+n 等于多少?

([email protected])[superdb]> select @@version;
+-----------+
| @@version |
+-----------+
| 8.4.0     |
+-----------+
1 row in set (0.00 sec)

-- 定义一个cte recursive 公用表达式t_sumtest 
-- num_sum     列标识计算的和
-- last_num    列标识当前数的前一个数值
-- current_num 列标识当前数的值

([email protected])[superdb]> WITH recursive t_sumtest (num_sum,last_num,current_num) as
    -> ( SELECT 1,0,1 FROM DUAL
    ->   UNION ALL
    ->   SELECT t1.num_sum+t1.last_num,t1.num_sum,t1.current_num+1 FROM t_sumtest t1 WHERE t1.current_num<10
    ->  )
    -> SELECT num_sum,last_num,current_num FROM t_sumtest;
+---------+----------+-------------+
| num_sum | last_num | current_num |
+---------+----------+-------------+
|       1 |        0 |           1 |
|       1 |        1 |           2 |
|       2 |        1 |           3 |
|       3 |        2 |           4 |
|       5 |        3 |           5 |
|       8 |        5 |           6 |
|      13 |        8 |           7 |
|      21 |       13 |           8 |
|      34 |       21 |           9 |
|      55 |       34 |          10 |
+---------+----------+-------------+
10 rows in set (0.00 sec)


([email protected])[superdb]> WITH recursive t_sumtest (num_sum,last_num,current_num) as
    -> ( SELECT 1,0,1 FROM DUAL
    ->   UNION ALL
    ->   SELECT t1.num_sum+t1.last_num,t1.num_sum,t1.current_num+1 FROM t_sumtest t1 WHERE t1.current_num<10
    ->  )
    -> SELECT num_sum,last_num,current_num FROM t_sumtest where current_num=10;
+---------+----------+-------------+
| num_sum | last_num | current_num |
+---------+----------+-------------+
|      55 |       34 |          10 |
+---------+----------+-------------+
1 row in set (0.01 sec)


标签:last,sum,t1,current,CTE,sumtest,num,MySQL8.0,累加
From: https://blog.csdn.net/zxrhhm/article/details/139195345

相关文章

  • docker安装MySQL8.0.35主从复制(实战保姆级)
    很久没有记录了,今天有时间就记录一下最近安装遇到的问题 liunx安装docker这个是前提,就不多过述1准备两台服务器10.104.13.13910.104.13.1402确保liunx安装的docker正常运行的ps:最好提前关闭防火墙或者提前开放对应端口不然同步会失败的关闭防火墙后记得重启docker......
  • mysql8.0 PXC安装配置
    一、环境初始化1.时间同步配置/etc/chrony.conf,添加如下:server0.centos.pool.ntp.orgiburst#指定时钟源allow192.168.43.0/24重启服务:systemctlrestartchronyd.service开机启动:systemctlenablechronyd查看状态:systemctlstatuschronyd验证端口:netstat......
  • mysql8.0+版本在使用group by 出现的问题
    起因:由于想使用MySQL8中的函数,手动将项目中的数据库从5.7升级到了8.0.20社区版本,但是升级完之后部分查询报错了,错误信息如下 whichisnotfunctionallydependentoncolumnsinGROUPBYclause;thisisincompatiblewithsql_mode=only_full_group_by去搜了一下,推荐的几......
  • How do I read Japanese characters from a PDF?
    HowdoIreadJapanesecharactersfromaPDF? EncodingCMapsinparticularforCJKscriptsareinaseparatepackage.For.Netuseitext7.font-asianvianuget.ForJavausecom.itextpdf:font-asianviamaven.Theexistenceofthispackageismorevisibl......
  • utools插件开发踩坑记录 - vite+recat搭建打包到utools环境时运行页面报错unexpected
    问题现象在本地开发环境时,运行无问题,一但打包到utools环境运行时,就出现了下面的现象依赖"dependencies":{"@ant-design/icons":"^5.3.7","antd":"^5.17.3","react":"^18.2.0","react-dom":"^......
  • Archery 平台执行DDL error:Threads_connected exceeds its critical threshold
    在数据库管理平台Archery上定时任务执行DDL操作,第二天发现报错,报错信息如下:Errorcopyingrowsfrom`DB`.`TABLE_NAME`to`DB`.`_TABLE_NAME_new`:Threads_connected=1358exceedsitscriticalthreshold1300登录查看Threads_connected信息,Threads_connected一直处于大......
  • Why the Pipe Character “|” Works in LangChain’s LCEL
    WhythePipeCharacter“|”WorksinLangChain’sLCELhttp://cncc.bingj.com/cache.aspx?q=python+pipe+operator&d=4965480815663428&mkt=en-US&setlang=en-US&w=ZTsip_Llmj7SCg1Xnjy71UfpBFEYqgVMIntroductionInLangChain,itisnowrecommendedto......
  • javascript去重累加
    /*对于"day1总杯数"字段的各行数据,进行去重累加*/functionconvertNullToZero(value){  if(isNaN(value)===true){    return0;  }else{    returnvalue;  }}vardata1=[];for(vari=0;i<data.length;i++){  varrow=data......
  • pytorch 踩坑,TypeError: expected seqence object with len>_0 or a single integer_typ
    CSDN搬家失败,手动导出markdown后再导入博客园在看Faster-R-CNN复现代码(https://blog.csdn.net/weixin_44791964/article/details/105739918)的时候,发现推理阶段报错,Dataparallel无法gather参考https://discuss.pytorch.org/t/nn-dataparallel-typeerror-expected-sequence......
  • 【jmeter】.SampleException: Mismatch between expected number of columns: 生成报
    1、问题现象Causedby:org.apache.jmeter.report.core.SampleException:Consumerfailedwithmessage:Consumerfailedwithmessage:Mismatchbetweenexpectednumberofcolumns:17andcolumnsinCSVfile:3,checkyourjmeter.save.saveservice.*configurationor......