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

边栏推荐
- The virtual reality real estate display system foresees the future decoration effect in advance
- 秒杀系统小demo
- Shuttle + Alluxio 加速内存Shuffle起飞
- 触发器简单解释
- 【OpenCV从入门到实践】图像处理技术[像素](全网最详细)
- 驱动页面性能优化的3个有效策略
- DNS的解析流程
- 从入门到精通的MySQL数据库视频教程
- 引领需求 为HR价值正名——“人力资源领先模型HRLM”成功首发
- View source and switch mirrors in two ways: npm and nrm
猜你喜欢

C语言操作符详解(2)

Analysis of port 9848 error at startup of Nacos client (non-version upgrade problem)

上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展

双重for循环案例(用js打印九九乘法表)

Stress testing and performance analysis of node projects

制作web3d动态产品展示的优点

虚拟现实房产展示系统提前预见未来装修效果

About the directory structure of the web application

BGP实验(路由反射器,联邦,路由优化)

nacos安装配置和单机部署教程
随机推荐
51 MCU Peripherals: Infrared Communication
C# 编码规范手册
npm 和 yarn的区别
C竞赛训练
mysql索引失效的常见9种原因详解
5款经典代码阅读器的使用方案对比
An advanced method for solving palindromes
MarkDown公式指导手册
科技赋能拉萨之“肺”,华为助力拉鲁湿地智慧管理守护绿水青山
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
The virtual reality real estate display system foresees the future decoration effect in advance
一文搞懂C操作符
Meta公司内部项目-RaptorX:将Presto性能提升10倍
淘系资深工程师整理的300+项学习资源清单(2021最新版)
Smart people's game improvement: Chapter 3, Lesson 2: "Number of Tongtong" (number)
DNS的解析流程
rhce作业
驱动页面性能优化的3个有效策略
程序员写PPT的小技巧
从入门到精通的MySQL数据库视频教程