当前位置:网站首页>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

边栏推荐
- 金蝶国际:半年亏掉去年一年,疯狂烧钱的商业模式如何持续
- flex布局(弹性布局)
- Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
- 51 MCU Peripherals: Infrared Communication
- Node installation and configuration of environment variables
- C 竞赛——捕鱼
- An advanced method for solving palindromes
- [Cartoon] 2021 full score programmer behavior comparison table (latest version)
- 路由规划中级篇
- 机器学习——支持向量机原理
猜你喜欢

触发器简单解释

Double for loop case (use js jiujiu printing multiplication table)

淘系资深工程师整理的300+项学习资源清单(2021最新版)

APT + Transform to realize multi module Application distributed Application life cycle

Different ways of shell scripting

HCIP第十七天

Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战

Tips for programmers to write PPT

程序员写PPT的小技巧

leetcode括号匹配问题——32.最长有效括号
随机推荐
秒杀系统小demo
路由规划中级篇
pl/sql之神奇的嵌套与变量生命周期
nacos registry
淘系资深工程师整理的300+项学习资源清单(2021最新版)
Contents of encoding-indexes.js file printed with Bluetooth:
机器学习——支持向量机原理
代码编世界 科技创未来
Linux CentOS8安装Redis6
eggjs controller层调用controller层解决方案
Features and installation of non-relational database MongoDB
BGP实验(路由反射器,联邦,路由优化)
Write implicit join on view with jOOQ 3.14 synthetic foreign key
Not annotated parameter overrides @NonNullApi parameter
What is the most important ability of a programmer?
Shuttle + Alluxio 加速内存Shuffle起飞
BGP+MPLS综合实验
蚂蚁三面:MQ 消息丢失、重复、积压问题,有哪些解决方案?
HCIP BGP综合实验 建立对等体、路由反射器、联邦、路由宣告及聚合
双重for循环案例(用js打印九九乘法表)