MySQL multi-table joint investigation and nested queries

1. Data Preparation

# ## insert data tables are created in preparation 

`` `Python 
# build table 
Create Table dep2 ( 
ID int, 
name VARCHAR ( 20 is ) 
); 

Create Table EMP2 ( 
ID int Primary Key AUTO_INCREMENT, 
name VARCHAR ( 20 is ), 
Sex enum ( ' MALE ' , ' FEMALE ' ) Not null default ' MALE ' , 
Age int, 
the dep_id int 
); 

# insert data 
iNSERT INTO dep2 values 
( 200 is, ' technical ' ), 
( 201,' HR ' ), 
( 202, ' sales ' ), 
( 203, ' operation ' ); 

INSERT INTO EMP2 (name, Sex, Age, the dep_id) values 
( ' Tank ' , ' MALE ' , 17,200 ), 
( ' Egon ' , ' FEMALE ' , 48,201 ), 
( ' Kevin ' , ' MALE ' , 38,201 ), 
( ' Jason ' ,'FEMALE ' , 28,202 ), 
( ' Owen ' , ' MALE ' , 18,200 ), 
( ' Sean ' , ' FEMALE ' , 18,204 ); 

# the PS: Yesterday explained how to split the field in accordance with the relationship table, the purpose of a more good management, table data are stored on the hard drive, memory is not a goal, the purpose is to take, so we have the data from the hard disk into memory, then we should be because they form one table to look more reasonable; 

# Note : split table query and then spliced together, can check the data from another table by a table; 
` 



# ## 1, associated with the query 

` `` MySQL 
# record left table and the right table a record corresponds again called -> "Cartesian product" PS: Baidu science 
# all data correspond again, although unreasonable but which have reasonable data, and now we need to do is to find a reasonable The data
View Code

2. 

. 1 , the Join Inner
 # . 1, the connection: only take two tables of correspondence between the recording 
SELECT * from EMP2 Inner emp2.dep_id the Join dep2 ON = dep2.id; 
SELECT * from EMP2 the Join Inner dep2 = ON emp2.dep_id dep2. ID and dep2.name = ' technology ' ;

 2 , left the Join
 # 2, the left connecting: reserved connecting the left table including no corresponding record on the basis of a relationship 
SELECT * from EMP2 left the Join dep2 oN emp2.dep_id = dep2.id;

 . 3 , right the Join
 # . 3, a right connection: the right table do not correspond to reserved recording on the basis of the inner connector 
SELECT * from EMP2 right emp2.dep_id the Join dep2 oN = dep2.id;

 . 4 , Union
# 4, all connected: Keep left on base, including connections, the right table does not record the correspondence between 
the SELECT * from EMP2 left the Join dep2 ON emp2.dep_id = dep2.id 
of Union 
the SELECT * from EMP2 right the Join dep2 ON emp2.dep_id = dep2.id; 
`` ` 
# ## 2, subqueries 

` `` MySQL 
# subquery condition is the result of a query statement parentheses, as with the other query statement to 

# 1 is the technical sector query human resources or employee information 
'' 
to obtain technical department and human resource id number, go to the staff table screened employee information to meet the requirements of the foregoing id; 
'' ' 
the SELECT * from EMP2 the WHERE dep_id in (the SELECT id from dep2 WHERE name = ' art '  or name = 'Human Resources ' ); 


# 2 each department recruits new idea: each department will check the latest recruits, press the corresponding departments on contingency table queries 
# check first emp table 
select t1.id, t1.name , t1.hire_date, t1.post, T2. * from EMP AS T1 
Inner the Join 
(SELECT POST, max (the hire_date) AS MAX_DATE from EMP Group by POST) AS T2 
ON t1.post = t2.post 
WHERE t1.hire_date = T2 .max_date;

Guess you like

Origin www.cnblogs.com/bigbox/p/12037233.html