MySQL--multi-table query

The relationship between tables: one-to
-one: for example, the t_person table and the t_card table, that is, people and ID cards. In this case, it is necessary to find out the master-slave relationship, that is, who is the master table and who is the slave table. A person may not have an ID card, but an ID card must have a talent, so the person is the master table, and the ID card is the slave table. There are two schemes for designing the slave table:
adding a foreign key column in the t_card table (relative to the t_user table), and adding a unique constraint to the foreign key;
adding a foreign key constraint to the primary key of the t_card table (relative to the t_user table), that is, the t_card table A primary key is also a foreign key.
To describe a one-to-one relationship, foreign keys can be added on either side.
One-to-many (many-to-one): The most common is one-to-many! One-to-many and many-to-one, which is the point of view. The relationship between t_user and t_section is one-to-many from the point of view of t_user, while from the point of view of t_section, it is many-to-one to
describe one-to-many or many-to-one relationships, and foreign keys are generally added on the more side.
Many-to-many: such as t_stu and t_teacher tables, that is, a student can have multiple teachers, and a teacher can also have multiple students. This situation usually requires the creation of intermediate tables to handle many-to-many relationships. For example, create another table t_stu_tea table and give two foreign keys, one relative to the t_stu table and the other relative to the t_teacher table.
Note: No matter what kind of relationship, it is ultimately the setting of foreign keys.

Multi-table query:
1. Inner join
a) cross join cross join (understand) will produce Cartesian product
b) natural join equal join (understand)
c) join using join table name on condition You can specify fields to eliminate the Cartesian product.
show inline
join on format: select * from table 1 join table 2 on condition.
select * from table 1 inner join table 2 on condition
implicit inner join
Select * from table 1, table 2 where condition;
inner join can only get related data.
2. Outer join:
OUTER JOIN ON
Left outer join LEFT OUTER JOIN ON [check all left table] Right outer join RIGHT OUTER JOIN ON [check all right table]
full outer join (not supported by MySQL) FULL JOIN natural join NATURAL JOIN
3. Subquery:
A select statement contains another complete select statement.
A subquery is a nested query, that is, a SELECT contains a SELECT. If there are two or more SELECTs in a statement, then it is a subquery statement.
Subqueries can appear anywhere: select, from, where. . .
1). A single-row, single-column subquery
is a value = > < >= <= !=
Find out the person in the same department as Xiao Liu
SELECT * FROM emp WHERE dept_id=(SELECT dept_id FROM emp WHERE NAME='Xiao Liu') AND NAME !='Xiao Liu';
2). A single-column and multi-row subquery
means multiple values. You can use in all any
to query the same employee information as the three departments
SELECT * FROM emp WHERE salary IN(SELECT salary FROM emp WHERE dept_id=3) AND dept_id!=3;
>any: greater than the minimum value in the subquery.
>all: Greater than the maximum value in the subquery.
<any: Less than the maximum value in the subquery.
<all: Less than the minimum value in the subquery.
>=any: Greater than or equal to the minimum value in the subquery.
>=all: Greater than or equal to the maximum value in the subquery.
<=any: Less than or equal to the maximum value in the subquery.
<=all: Less than or equal to the minimum value in the subquery.
!=any or <>any: not equal to any value in the subquery.
!=all or <>all: Not equal to all values ​​in the subquery.
=any: Equal to any value in the subquery.
=all: equals all values ​​in the subquery (meaningless).
3). Multi-row and multi-column subqueries (single row and multi-column)
can be processed as a table.
Display the employee information of all departments with id<3, and display the department name
SELECT * FROM emp ,(SELECT * FROM dept WHERE id<3) mydept WHERE emp.dept_id=mydept.id;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326528418&siteId=291194637