当前位置:网站首页>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
边栏推荐
- Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战
- About the directory structure of the web application
- Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
- 机器学习——支持向量机原理
- mysql高阶语句(一)
- Kingdee International: Lost in half a year and last year, how does the business model of frantically burning money continue
- leetcode-318.最大单词长度乘积
- npm 和 yarn的区别
- An advanced method for solving palindromes
- 上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
猜你喜欢
随机推荐
Analysis of port 9848 error at startup of Nacos client (non-version upgrade problem)
Difference and analysis of CPU usage and load
Nacos database configuration
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
leetcode括号匹配问题——32.最长有效括号
Machine learning -- - theory of support vector machine (SVM)
mysql索引失效的常见9种原因详解
Practice on optimizing startup performance of VS Code
How to install the specified version package with NPM and view the version number
VMTK环境配置记录
OAuth 授权协议 | 都云原生时代了,我们应该多懂一点OAuth ?
zabbix自动发现和自动注册
关于 VS Code 优化启动性能的实践
What are the ways to improve software testing capabilities?After reading this article, it will take you up a notch
金蝶国际:半年亏掉去年一年,疯狂烧钱的商业模式如何持续
使用jOOQ 3.14合成外键在视图上写隐式连接
程序员最重要的能力是什么?
leetcode-338.比特位计数
Common functions of pytorch
MySQL联合查询(多表查询)