当前位置:网站首页>MySQL takes the union of two query conditions and then queries
MySQL takes the union of two query conditions and then queries
2022-07-19 04:55:00 【Xiaoxinai programming】
Catalog
Problem analysis :
such sql It sounds a little complicated , But when you think about it carefully, it's actually a very simple sql, First find out what you need sqlselect dept_id from sys_dept sd where AREACODETYPE ='4' and AREACODE ='1111'
And then we sql The first condition is found , And put this sql Continue to query as a condition
select role_id from sys_role_dept srd,
(select dept_id from sys_dept sd where AREACODETYPE ='4' and AREACODE ='1111') a
where srd.dept_id = a.dept_id

Then you can continue the association query
select user_id from sys_user_role sur,(select role_id from sys_role_dept srd,
(select dept_id from sys_dept sd where AREACODETYPE ='4' and AREACODE ='1111') a
where srd.dept_id = a.dept_id) b where sur.role_id = b.role_id

Then the first result came out , Then we check the second result
Similar to the above logic , Finally, I won't explain the code block
select user_id from sys_user_dept sud,
(select dept_id from sys_dept sd where AREACODETYPE ='4' and AREACODE ='1111') a
where sud.dept_id = a.dept_id

Problem solving
Then the results of the first column and the second column are different , But there are similarities , Then at this time, we can carry out union value .
select a.user_id as dept_id from (select user_id from sys_user_role sur,(select role_id from sys_role_dept srd,
(select dept_id from sys_dept sd where AREACODETYPE ='4' and AREACODE ='1111') a
where srd.dept_id = a.dept_id) b where sur.role_id = b.role_id) a,(select user_id from sys_user_dept sud,
(select dept_id from sys_dept sd where AREACODETYPE ='4' and AREACODE ='1111') a
where sud.dept_id = a.dept_id)b where a.user_id = b.user_id

summary
The main thing is to use mysql Find out the data , Then treat the data as a new table file , And then we're working on it , Treat the data as another table file , But there are better ways to deal with it , I didn't write it here , You can provide some ideas ,
left join finger :
select * from a left join b on a.aid = b.bid
Take it out first a All data in the table , Then add and a,b Matching data
What is taken out at this time is :
1 a1 b1
2 a2 b2
3 a3 Null character
The result set of the left out join includes LEFT OUTER All rows of the left table specified in Clause , Not just the rows that the join columns match . If a row in the left table does not match a row in the right table , All selection list columns in the right table in the associated result set row are null .
边栏推荐
- Warriors of the Visual Studio, Assemble! (Visual Studio的勇士们,汇编吧!) 原创 2009年07月12日 19:40:00 标签:汇编 /mic
- tidb 学习
- UE-插件 ElectronicNodes 5.0.0/4.23-4.27
- 2022 latest version of campus errand applet source code
- 畢設:基於Vue+Socket+Redis的分布式高並發防疫健康管理系統
- redis 安装
- 微服务高并发服务治理
- JS中substr与substring的区别
- Construction and application of knowledge atlas de (V): knowledge reasoning
- 用FastApi进行WEB开发
猜你喜欢

Database learning notes (I) retrieval data

MYSQL两个查询条件取并集然后进行查询

【Lipschitz】基于matlab的Lipschitz李氏指数仿真

UE-插件 ElectronicNodes 5.0.0/4.23-4.27

UE plug-in electronicnodes 5.0.0/4.23-4.27

一文了解Zipkin

Penetration test 10 - scan web directories (dirb, wfuzz, wpscan, Nikto)

mysql主从架构和读写分离、以及高可用架构

TopicExchange交换机简单使用。

Project structure of wechat applet
随机推荐
Multiple connections will be maintained for each provider instance
Beidou clock server (NTP server) makes the time of college entrance examination more accurate
2022最新版校园跑腿小程序源码
shardingsphere内核原理
数据库取配置文件字段,然后进行数据处理和判断
新生任务-5
Using circular statements to make login programs
Masm32 writer
浅聊全局过滤器
Redis简介
.sh脚本编写
Redis 集群面试题
简洁UI好玩的文字转换emoji表情微信小程序支持句子词语转换_源码
Common PostgreSQL data operation notes (updated from time to time)
Constraints on MySQL tables (Basics)
免签名模板审核的短信验证测试
老年祝福火爆短视频微信小程序源码下载支持流量主
[FPGA tutorial case 26] realize the basic operation of decimals through Verilog in FPGA
swagger的坑
FanoutExchange交换机简单使用