当前位置:网站首页>MYSQL两个查询条件取并集然后进行查询
MYSQL两个查询条件取并集然后进行查询
2022-07-17 05:02:00 【小新爱编程】
问题分析:
这种sql听着有点复杂,但是仔细想一下其实是个很简单的sql,首先查出来需要的sqlselect dept_id from sys_dept sd where AREACODETYPE ='4' and AREACODE ='1111'
然后咱们sql第一个条件查出来了,然后把这个sql当成条件继续进行查询
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

然后可以继续进行关联查询
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

然后第一个结果出来了,然后我们查第二个结果
和上面逻辑差不多,最终代码块我就不解释了
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

问题解决
然后第一列和第二列他们的结果是不一样的,但是有相似地方,然后这个时候我们就可以进行并集取值了。
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

总结
主要就是采用mysql查询出数据,然后将数据当作一个新的表文件,然后在进行处理,在将数据当作另一个表文件进行处理,但是这样也有更好的办法进行处理,这里我就没写出来,可以提供一下思路,
left join 指:
select * from a left join b on a.aid = b.bid
首先取出a表中所有数据,然后再加上与a,b匹配的的数据
此时的取出的是:
1 a1 b1
2 a2 b2
3 a3 空字符
左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。
边栏推荐
猜你喜欢
![[Lipschitz] simulation of Lipschitz Lipschitz exponent based on MATLAB](/img/72/c69ed6e5538169c362b7b4bab36d5e.png)
[Lipschitz] simulation of Lipschitz Lipschitz exponent based on MATLAB

MySQL one line to many lines (split according to specific symbols)
![Money making master applet [latest version 5.9.9] mall / instant withdrawal of commission / distribution promotion / phone recharge / is meituan hungry for takeout](/img/8b/29027c2dee4ef764bb2e4b5b499a23.jpg)
Money making master applet [latest version 5.9.9] mall / instant withdrawal of commission / distribution promotion / phone recharge / is meituan hungry for takeout

Mongo Db单机版的安装和快速使用

ThreadLocal线程安全示例及其原理

Emqx pressure test tread pit for your reference

mysql优化

shardingproxy分库分表实战及同类产品对比

Summary of black screen problems in unity UMP packaging

Extreme video compression using the pellet toolbox
随机推荐
力扣刷题02(三数之和+最大子序和+二叉树最近公共祖先)
PingCAP Clinic 数据采集说明
Record a stored procedure to batch modify the table structure
Tasking 新建 AURIX TC37X demo 工程
Water and electricity meter reading and recharge management system in Colleges and universities in property community
MySQL one line to many lines (split according to specific symbols)
Usage scenarios and usage of judgment and rounding down in MySQL
Minio installation, deployment and use
An easy-to-use network liar reporting system without encrypted version source code
Kubernetes 的监控与告警
Efficient insertion of references in word with thousands of words and many pictures
tidb 学习
Unity UMP打包黑屏問題總結
毕设:基于Vue+Socket+Redis的分布式高并发防疫健康管理系统
Expected to replace deepfake? Uncover this year's super popular nerf Technology
Notes on Advanced Mathematics: a conjecture about the Equivalent Infinitesimal Substitution
Rearrange data according to date JS
记录一次存储过程批量修改表结构
Mongo Db聚合操作和索引
[vuforia] simple logic of image recognition