当前位置:网站首页>MySQL复杂查询使用临时表/with as(类似表变量)
MySQL复杂查询使用临时表/with as(类似表变量)
2022-04-23 02:39:00 【一只努力xx的程序媛】
查询需求:
如果第一个SQL能查出结果,则返回结果,否则,执行第二条SQL,返回第二条SQL的结果。
SQL Server中使用表变量的方式:
如果查出有“wangwang”用户则返回,否则查询“zhangzhang”用户的id返回。有优先级
declare @temp table(id int)
insert into @temp
select id
from user
where name = 'wangwang'
if not exists(select id from @temp)
begin
select id
from user
where name='zhangzhang'
end else
begin
select id from @temp
end
MySQL中没有表变量,只能使用临时表代替:
CREATE TEMPORARY TABLE temp1(id int ); -- 创建临时表1
insert into temp1
select id
from user
where name='wangwang';
CREATE TEMPORARY TABLE temp2(id int ); -- 创建临时表2
insert into temp2
select id
from user
where name='zhangzhang';
CREATE TEMPORARY TABLE temp1_re select id from temp1; -- 复制临时表1
select (case when (SELECT count(*) FROM temp1_re)>0 then -- 使用case when判断
(select id from temp1 order by id desc limit 1) else (
select id from temp2 order by id desc limit 1) end ) id ;
注意:
(1)需要复制临时表1,是因为“在同一个query语句中,你只能查找一次临时表”,否则报错“can’t reopen ”。针对表中数据不多的情况下,可以复制表进行操作。
(2)因为要在mybatis的xml中执行多条SQL,数据库连接需要设置allowMultiQueries=true。
2019-10-29更新:
发现MySQL有个类似SQLserver中的表变量的语句,那就是with as,也叫做子查询部分(subquery factoring),可以定义一个SQL片断,该SQL片断会被整个SQL语句用到,例如:
with temp_a as(
select id,name from user where name='wangwang'
),
temp_b as(
select id,name from user where name='zhangzhang'
)
select (case when (SELECT count(*) FROM temp_a)>0 then -- 使用case when判断
(select id from temp_a order by id desc limit 1)
else
(select id from temp_b order by id desc limit 1)
end
) id
这种写法是MySQL 8.0的特性
参考:https://www.cnblogs.com/Niko12230/p/5945133.html
https://stackoverflow.com/questions/324935/mysql-with-clause
版权声明
本文为[一只努力xx的程序媛]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_23888451/article/details/102686280
边栏推荐
- Develop a chrome plug-in from 0 (2)
- Fast and robust multi person 3D pose estimation from multiple views
- 高效音乐格式转换工具Music Converter Pro
- Web learning record (medium)
- Those years can not do math problems, using pyhon only takes 1 minute?
- 一个国产图像分割项目重磅开源!
- MySQL C language connection
- How to solve the complexity of project document management?
- The usage and difference of * and & in C language and the meaning of keywords static and volatile
- 1215_ Hello world used by scons
猜你喜欢

机器学习(周志华) 第十四章概率图模型

RT_Thread自问自答

MySQL JDBC programming

The importance of ERP integration to the improvement of the company's system

Interim summary (Introduction + application layer + transportation layer)
![Handwritten memory pool and principle code analysis [C language]](/img/9e/fdddaa628347355b9bcf9780779fa4.png)
Handwritten memory pool and principle code analysis [C language]

LeetCode 1450 - 1453

十六、异常检测
![PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]](/img/ae/a6681df6c3992c7fd588334623901c.png)
PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]

JVM类加载器
随机推荐
wordpress 调用指定页面内容详解2 get_children()
After idea is successfully connected to H2 database, there are no sub files
Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
LeetCode 1450 - 1453
php+mysql對下拉框搜索的內容修改
Day 3 of learning rhcsa
Rhcsa day 1 operation
Intelligent agricultural management model
They are all intelligent in the whole house. What's the difference between aqara and homekit?
MySQL C language connection
在MySQL Workbench中执行外部的SQL脚本,报错
[NK] Niuke monthly race 48 D
小程序 canvas 画布半圆环
谷雨
IAR嵌入式开发STM32f103c8t6之点亮LED灯
If you want to learn SQL with a Mac, you should give yourself a good reason to buy a Mac and listen to your opinions
Rhcsa second day operation
Applet reads files
Want to experience homekit smart home? Why don't you take a look at this smart ecosystem
Halo open source project learning (I): project launch