Detailed MySQL database - multi-table queries - In connection, outer joins, sub-queries, the relevant sub-queries

Multi-table query

Single select statement extracted from the query results related to the plurality of tables, multi-table joins are usually based on the table with a parent-child relationship;

A cross-connect

All rows in the first table multiplied by all the rows in the second table, which is the Cartesian product
to create a consumer with the customer table:
code is as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- create table customers(
-- id int primary key auto_increment,
-- name VARCHAR(20)not null,
-- address VARCHAR(20)not NULL
-- );
-- CREATE table orders(
-- order_namre VARCHAR(20) primary key,
-- num char(20) not NULL,
-- price int not null,
-- customers_id int,
-- constraint cus_ord_fk FOREIGN key(customers_id) REFERENCES customers(id)
-- )
自己插入数据即可。
语法:

Implicit grammar (do not use keywords): select * from customers, orders;

Results are as follows:

Explicit syntax (keywords): select * from customers c INNER JOIN orders o ON c.id = o.customer_id;

Like two operating results, but the Cartesian product has an error, the following correction method

The connector 2

Because the cross-connect obtained result set is wrong. Thus the connection is based on the cross-connect
list only connected with the connection condition table matches the data line does not match the records will not be listed.

grammar:

Implicit Syntax:

?
1
select * from customers,orders where customers.id=orders.customers_id;

Explicit syntax:

?
1
select * from customers c INNER JOIN orders o ON c.id=o.customer_id;

Results are as follows

We can also play an alias to the program:
code is as follows:

?
1
2
select * from customers as c,orders o where c.id=o.customers_id;
SELECT * from customers as c inner join orders o on c.id=o.customers_id;

An outer connector 3

En only list information for all users of the purchased goods, does not list the user does not buy goods.
The outer link is a table is a base table, other tables Splicing of this message, if there is stitching on the last, if not display null; are linked into left outer join and right lower connection.
Left outer join: to the left of the keyword table for the base table for splicing

grammar:

?
1
select * from customers c LEFT JOIN orders o ON c.id=o.customer_id;

Right external connection: the right to form the keyword table for the group

grammar:

?
1
select * from orders o RIGHT JOIN customers c ON c.id=o.customer_id;

4 subquery

In some cases, when querying, the conditions required is another result of a select statement, this time it will use the sub-query, in order to give the main query (outer query) to provide data and first performed (internal inquiry) It called subqueries; nested subqueries divided into sub-queries and correlated subquery.

Nested subqueries:

Performing internal query is independent of the outer query, the query is only executed once inside, after finished as a result of conditions external query using a (sub nested subquery query may run out separately.)

Grammar and Exercise: check out all the student id taught for teachers 1.

?
1
select * from students where id in ( select s_id from teacher_student where t_id=1);

Correlated subquery:

Implementation of internal query depends on the outer query data, each outer query execution time, the internal inquiry will be executed once. Every time the first outer query execution, remove a tuple external lookup table, the data transmission in the current tuple to an internal query, and then execute the inner query. According to results of the internal inquiry carried out to determine where the condition in the outer query whether the current tuple met, if it is to meet the current requirements of the tuple is a record of compliance with, or do not meet the requirements. Then, remove the outer query continues to the next tuple data, performing the above operations until all tuples are processed.
Create three tables
Exercise 1. check out the id of 1 teacher taught all students.
- Create table teacher

?
1
2
3
4
5
create table teacher1(
id int primary key auto_increment,
name char (20) not NULL ,
subject char (20) not null
);

- Create Student Table

?
1
2
3
4
5
create table student1(
id int primary key auto_increment,
name char (20) unique not null ,
age int null
);

- Create a third table

?
1
2
3
4
5
6
7
8
9
create table tea_stu(
id int PRIMARY KEY ,
name char (20),
t_id int ,
s_id int ,
score int not null ,
constraint teacher1_id_fk foreign key (t_id) references teacher1(id),
constraint student_id_fk foreign key (s_id) references student1(id)
);

练习1. 查询出id为1的老师教过的所有学生。

做法1 用分开的方法写出来:

?
1
2
select s_id from tea_stu where t_id=1;
select * from student1 where id in (2,3);

做法2:

?
1
select * from student1 where id in ( select s_id from tea_stu where t_id=1);

相关子查询:

内部查询的执行依赖于外部查询的数据,外部查询每执行一次,内部查询也会执行一次。每一次都是外部查询先执行,取出外部查询表中的一个元组,将当前元组中的数据传递给内部查询,然后执行内部查询。根据内部查询执行的结果,判断当前元组是否满足外部查询中的where条件,若满足则当前元组是符合要求的记录,否则不符合要求。然后,外部查询继续取出下一个元组数据,执行上述的操作,直到全部元组均被处理完毕。
求:每一科考试成绩大于平均分的学生的分数。

?
1
select * from tea_stu as a where a.score>( select avg (b.score) from tea_stu as b where a.s_id=b.s_id);

以上所述是小编给大家介绍的MySQL多表查询详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

                    <div class="art_xg">
                    <h4>您可能感兴趣的文章:</h4><ul><li><a href="/article/75456.htm" title="MySQL中基本的多表连接查询教程" target="_blank">MySQL中基本的多表连接查询教程</a></li><li><a href="/article/107813.htm" title="mysql delete 多表连接删除功能" target="_blank">mysql delete 多表连接删除功能</a></li><li><a href="/article/148401.htm" title="mysql多表连接查询实例讲解" target="_blank">mysql多表连接查询实例讲解</a></li><li><a href="/article/177142.htm" title="MySQL多表连接的完美实例教程" target="_blank">MySQL多表连接的完美实例教程</a></li></ul>
                    </div>
					<p class="tip">如您对本文有所疑义或者对本文内容提供补充建议,请联系小编<a href="http://wpa.qq.com/msgrd?v=3&amp;uin=2998481778&amp;site=jb51net&amp;menu=yes" target="_blank"><img border="0" src="https://pub.idqqimg.com/qconn/wpa/button/button_111.gif" alt="点击这里给我发消息" title="点击这里给我发消息"></a>,本站会保留修改者版权</p><div class="lbd_bot clearfix">
					<div id="_ifawebpf8ce" style=""><iframe width="820" frameborder="0" height="250" scrolling="no" src="https://pos.baidu.com/s?hei=250&amp;wid=820&amp;di=u4846790&amp;ltu=https%3A%2F%2Fwww.jb51.net%2Farticle%2F159634.htm&amp;psi=2cf4d6fa3c0f93cfba5f3d5f3ebd4ab3&amp;ari=2&amp;cja=false&amp;cec=GBK&amp;prot=2&amp;ti=%E8%AF%A6%E8%A7%A3MySQL%E6%95%B0%E6%8D%AE%E5%BA%93--%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2--%E5%86%85%E8%BF%9E%E6%8E%A5%EF%BC%8C%E5%A4%96%E8%BF%9E%E6%8E%A5%EF%BC%8C%E5%AD%90%E6%9F%A5%E8%AF%A2%EF%BC%8C%E7%9B%B8%E5%85%B3%E5%AD%90%E6%9F%A5%E8%AF%A2_Mysql_%E8%84%9A%E6%9C%AC%E4%B9%8B%E5%AE%B6&amp;drs=1&amp;pis=-1x-1&amp;ant=0&amp;dis=0&amp;cfv=0&amp;cpl=21&amp;pcs=1899x941&amp;psr=1920x1080&amp;par=1920x1030&amp;ccd=24&amp;tpr=1580798985393&amp;ps=5886x364&amp;dc=3&amp;cmi=33&amp;cce=true&amp;dri=0&amp;dai=1&amp;dtm=HTML_POST&amp;tcn=1580798985&amp;chi=1&amp;ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DFk8UwWVXXnmBsHJnlli9U8yEm1nIxsbT-qcQbPXYc2yvJTHxLvU2P1kNbr3kbUqHTyH1_aNmI_vNXt27d19AW_%26wd%3D%26eqid%3D882c4fd70034142a000000035e3913ae&amp;tlm=1580798985&amp;pss=1899x7769&amp;exps=111000,110011&amp;col=zh-CN&amp;cdo=-1"></iframe></div><script type="text/javascript" src="//jscode.jbzj.com/production/ql/common/h/source/n/h/production/kmtr.js"></script>

					</div>
				<p>原文链接:https://blog.csdn.net/python20180218/article/details/89192518</p><div class="jb51ewm"><div class="fl"><img src="//files.jb51.net/skin/2018/images/jb51ewm.png"></div><div class="fr"><p>微信公众号搜索 “ <span>脚本之家</span> ” ,选择关注</p><p>程序猿的那些事、送书等活动等着你</p></div></div></div>
发布了45 篇原创文章 · 获赞 0 · 访问量 3554

Guess you like

Origin blog.csdn.net/qq_44813090/article/details/104169636