当前位置:网站首页>MySQL complex query uses temporary table / with as (similar to table variable)
MySQL complex query uses temporary table / with as (similar to table variable)
2022-04-23 02:41:00 【An effort to XX program yuan】
Query requirement :
If the first one SQL Can you find out the result , Returns the result , otherwise , Implement Article 2 SQL, Return to Article 2 SQL Result .
SQL Server How to use table variables in :
If you find out “wangwang” The user returns , Otherwise, query “zhangzhang” User id return . Priority level
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 There are no table variables in the , Only temporary tables can be used instead of :
CREATE TEMPORARY TABLE temp1(id int ); -- Create a temporary table 1
insert into temp1
select id
from user
where name='wangwang';
CREATE TEMPORARY TABLE temp2(id int ); -- Create a temporary table 2
insert into temp2
select id
from user
where name='zhangzhang';
CREATE TEMPORARY TABLE temp1_re select id from temp1; -- Copy temporary table 1
select (case when (SELECT count(*) FROM temp1_re)>0 then -- Use case when Judge
(select id from temp1 order by id desc limit 1) else (
select id from temp2 order by id desc limit 1) end ) id ;
Be careful :
(1) Need to copy temporary table 1, Because “ In the same query In the sentence , You can only look up the temporary table once ”, Otherwise, the report will be wrong “can’t reopen ”. When there is not much data in the table , You can copy tables for operations .
(2) Because in mybatis Of xml More than one SQL, Database connection needs to be set allowMultiQueries=true.
2019-10-29 to update :
Find out MySQL There is a similar SQLserver Statements of table variables in , That's it with as, Also called subquery part (subquery factoring), You can define a SQL fragment , The SQL The fragment will be replaced by the whole SQL Statement use , for example :
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 -- Use case when Judge
(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
This way of writing is MySQL 8.0 Characteristics of
Reference resources :https://www.cnblogs.com/Niko12230/p/5945133.html
https://stackoverflow.com/questions/324935/mysql-with-clause
版权声明
本文为[An effort to XX program yuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230239165816.html
边栏推荐
猜你喜欢
Real math problems in 1958 college entrance examination
After idea is successfully connected to H2 database, there are no sub files
Day 3 of learning rhcsa
leangoo脑图-共享式多人协作思维导图工具分享
MySQL JDBC编程
SQL server2019 cannot download the required files, which may indicate that the version of the installer is no longer supported. What should I do
This is how the power circuit is designed
Rhcsa day 1 operation
打靶narak
Huashu "deep learning" and code implementation: 01 Linear Algebra: basic concepts + code implementation basic operations
随机推荐
解决win7 中powershell挖矿占用CPU100%
Implementation of distributed scenario business operation log (based on redis lightweight)
Synchronized lock and its expansion
Rhcsa day 3 operation
Program design: l1-49 ladder race, allocation of seats (simulation), Buxiang pill hot
So library dependency
Interim summary (Introduction + application layer + transportation layer)
进阶上将程序员必备素质
Store consumption SMS notification template
How big the program development of single chip microcomputer project can be, it represents your level of knocking code
[suggestion collection] hematemesis sorting out golang interview dry goods 21 questions - hanging interviewer-1
Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23
Global, exclusive and local routing guard
【2019-CVPR-3D人体姿态估计】Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views
Day 3 of learning rhcsa
[XJTU计算机网络安全与管理]第二讲 密码技术
Hack the box optimum
First day of rhcsa
Usage of vector common interface
Flink stream processing engine system learning (III)