当前位置:网站首页>MySQL union query (multi-table query)
MySQL union query (multi-table query)
2022-08-02 06:45:00 【hungry very hungry】
One, inner connection
select *from table 1 [ inner | cross ] join table 2 [ on filter condition ] [ where query condition ];
[ inner | cross ]:
join inner join keyword (must have);
on can be omitted syntactically, but if it is omitted, it will query the Cartesian product of multiple tables;
1.select *from table 1 join table 2 [ on filter condition ] [ where query condition ];
2.select *from table 1, table 2 [ where query condition ];
3.select *from table 1 inner join table 2 [ on filter condition ] [ where query condition ];
4.select *from table 1 cross join table 2 [ on filter condition ] [ where query condition ];
(1.2 is used more; multi-table query is better to use 1)


Example 1: Query Zhang San's grades:
1. Perform inner join query (Cartesian product)
select s.*,st.* from student s join score_table st;

2. Remove invalid data (on filter condition)
select s.*,st.* from student s join score_table st on s.id=st.student_id;

3. Query Zhang San's results (where condition)
select s.*,st.* from student s join score_table st on s.id=st.student_id where s.username='Zhang San';

Example 2: Query each person's grades, subject names, personal information:
1. Join table query (three tables) select *from table 1 join table 2 join table 3;
2. Filter meaningless data in Cartesian product: select *from table 1 join table 2 [ on conditional filtering ] join table 3 [ on conditional filtering ];
select s.username,s.sn,s.mail,st.score,c.namefrom score_table st join course c on st.course_id=c.id join student s on s.id=st.student_id;
Second, outer join:
1. Left (outer) connection:
select * from table 1 left join table 2 on join condition [where condition query];
The query result of Table 1 is all data, and the query result of Table 2 is the data that overlaps with Table 1

2. Right (outer) connection
select * from table 1 right join table 2 on join condition [where condition query];

Left/right joins can be implemented with each other, just swap the order of the tables.


The difference between on and where in join table query:
1.on can be omitted in inner join, but not in outer join;
2.on has different effects in inner join and outer join;
left join...on query cannot filter out the data in the left table, while inner join on query can filter out global data.
3. On and where are different in outer joins
on filters the Cartesian product filter conditions, where filters specific businesses
Three, self-connection (self-query)
select *from table name as t1, table name as t2 where t1.id=t2.id [, …]
Example 1: Querying English Scores (1) Find out the subject ID according to the subject name (there is only the subject ID in the score sheet, but no subject name) (2) Self-inquiry (3) Remove meaningless data in Cartesian product (meaningful data: the same primary key;) (4) Set the where condition so that Table 1 only queries English scores, and Table 2 queries computer scores (5) Set the where multi-condition query to let the English score Four, sub-query (nested query) select field name from Example: Check Zhang San's classmates Five, merge query (at least two tables) union (merge the result set, and deduplicate, only keep one of the duplicate data) union all (will not remove duplicate rows from the result set) Example: query for courses with id less than 3 and name "English" select * from course where id<3 union select * from course where name='English'; Let me introduce myself first. The editor graduated from Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Ali in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials

边栏推荐
- OAuth 授权协议 | 都云原生时代了,我们应该多懂一点OAuth ?
- leetcode solves the linked list merge problem in one step
- 物联网如何改变城市运行效率
- What is the most important ability of a programmer?
- C language entry combat (13): decimal number to binary
- Not annotated parameter overrides @NonNullApi parameter
- zabbix自动发现和自动注册
- c语言指针运算
- Nacos安装详细过程
- MarkDown Formula Instruction Manual
猜你喜欢

Write implicit join on view with jOOQ 3.14 synthetic foreign key

pl/sql之神奇的嵌套与变量生命周期

点云旋转到参考坐标系方向(最小方向包围盒方法)

腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践

Differences between i++ and ++i in loops in C language

oracle 远程连接数据库

机器学习——支持向量机原理

APT + Transform 实现多模块应用Application生命周期分发

51 microcontroller peripherals article: dot-matrix LCD

Automated operation and maintenance tools - ansible, overview, installation, module introduction
随机推荐
洛谷小游戏大全(用洛谷的人都得知道)
如何优化OpenSumi终端性能?
npm、nrm两种方式查看源和切换镜像
Nacos database configuration
Redis database
目标检测重要概念——IOU、感受野、空洞卷积、mAP
Tips for programmers to write PPT
npm 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
npm 和 yarn的区别
股价屡创新低 地产SaaS巨头陷入困境 明源云该如何转型自救?
How to perform concurrent calculation (stability test and stress test)?
5款经典代码阅读器的使用方案对比
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
Double for loop case (use js jiujiu printing multiplication table)
npm、cnpm的安装
What is the most important ability of a programmer?
APT + Transform 实现多模块应用Application生命周期分发
Analysis of the source code of the JS UI framework of Hongmeng system
MySQL(3)
pytorch basic operations: classification tasks using neural networks