MySQL - join query and subquery

1. Connection query

Single-table query: Querying data in a table is called single-table query.

Join query, combining two (multiple) tables, querying data in two (multiple) tables, querying part of one table, and querying another part of data in another table, this cross-table query, connecting multiple tables The method of querying data from a table is called a join query!

Connection query is divided into: SQL92 syntax, SQL99 syntax. At present, the mainstream uses SQL99 syntax.

The grammatical structure of SQL92 is more rough, and the grammatical structure is not clear. The judgment of table connection and other conditions are placed after where, which is very confusing. select.....from.....where....

The grammatical structure of SQL99 is clear. A certain table is connected with another table using join, and then the judgment condition is placed after on. select.....from...inner join.....on...inner join...on...where...(inner can be omitted, but add The above grammatical structure is clearer)

Cartesian product phenomenon:

When connecting two tables for query, if there is no limit, the number of queries is the product of all records in the two tables!

At this time, the role of the constraints of internal and external connections is reflected!

1. Inner connection:

Equivalent connection: the query result is determined by the equal sign of the where condition

Although the desired results were queried above, the number of queries still did not decrease, it was just filtered out. At the same time, we also know that the fewer the number of connected tables in the future, the better, otherwise the efficiency will be reduced!

Non-equivalence join:

Self-join: One table is treated as two tables.

2. Outer join (right/left + outer[outer can be omitted]):

The difference between inner join and outer join: inner join has no primary and secondary relationship, two (multiple) tables are at the same level, while outer join has a primary and secondary relationship, mainly to check what, and secondly to check what!

Left outer join (left): Indicates that the table on the left of the join keyword is the main table, mainly to find out the data in the left table, and by the way, also find out the data in the right table.

Right outer join (right): Indicates that the table on the right of the join keyword is the main table, mainly to find out the data in the right table, and by the way, also find out the data in the left table.

Summary: The number of queries for outer joins >= the number of queries for inner joins.

Multi-table query:

使用select .....from ......join....on......join....on.....where.....group by .......having........order by .......

For example, check: Find out the department name and salary level of each employee, and the superior leader, display the employee's name, leader name, department name, salary, and salary level.

select e.ename,d.dname,n.ename as '上级',e.sal,s.grade from emp e join dept d on e.deptno = d.deptno left join emp n on e.mgr = n.empno join salgrade s on e.sal between s.losal and s.hisal;

3. Full connection (rarely used)


Two, sub query

Subquery: The select statement is nested in the select statement, and the nested select statement is called a subquery.

Subquery statements can be placed behind select, from, where.

A subquery appears in the where clause:

For example: Query the name and salary of employees whose salary is greater than 800 yuan

A subquery appears in the from clause:

Note: The subquery in from can be a temporary table among the things found in the subquery.

Example: Find out the pay grade for the average salary for each job?

select t.*,s.grade from (select job,avg(sal) as avgsal from emp group by job) t join salgrade s on t.avgsal between s.losal and s.hisal;

select t.*,s.grade from (select ename,avg(sal) as avgsal from emp) t join salgrade s on t.avgsal between s.losal and s.hisal;

Subqueries appearing in the select clause:

Query the department name of each employee and ask to display the department name and employee name?

select e.ename,e.deptno,(select dname from dept join emp on emp.deptno = dept.deptno) from emp e;

Note: The result returned by the subquery after select can only return one result. When there are more than one result, an error will be reported! ! !

Guess you like

Origin blog.csdn.net/m0_73968621/article/details/132678288