MySQL query -------------- Advanced 6: join query

Advanced # 6; join query 
/ * 
Meaning: connect multiple tables, when the field queries from multiple tables when the connection will be used queries 
I feel here like vlookup function excel in the 

Cartesian product of the phenomenon: Table 1 m rows, table 2, there are n rows, resulting m * n rows 

how did it happen: no valid connection conditions 
how to solve: Add a valid connection conditions 
Category: 
	chronological classification 
	sql92 criteria: only the inner connection 
	sql99 standard [recommended]: an outer support connected to the connection + (left and right outer outside) + cross-connect 

	according to functional classification: 
		the connection: equi-equivalent non-connected, the connection from 

		the external connection: left outer join, right outer join, full outer join 

		cross connection 
* / 
use Girls; 
the SELECT * from Beauty; # 12 line 
select * from boys; # 4 rows 
# Cartesian product of the phenomenon appears 
select name, boyName from beauty, boys ; # 48 line, this is wrong 
# correct wording 
select name, Beauty from boyName, Boys WHERE beauty.boyfriend_id = boys.id; 

# a, SQL92 standard 
. equijoins # 1 
/ *  
(1) equivalent results table is connected to a plurality of multi-table intersection
(2) connected to n-table, at least n-1 connections conditions 
(3) does not require the order of multi-table 
(4) starting alias table typically 
have said before (5) can be used with grammar, e.g. order by, group by other 

* / 
# case 1: query goddess name and the corresponding male name of God 
the SELECT name, boyName from Beauty, Boys the WHERE beauty.boyfriend_id = boys.id; 

# case 2: query employee name and department name corresponding 
use MyEmployees; 
the SELECT last_name, from department_name the Employees, Departments WHERE employees.department_id = departments.department_id; 

# 2 is a table alias. 
/ * 
(1) improve the conciseness of the statement 
(2) distinguishing a plurality of the same name field 

Note: If the starting alias tables, the query field can not be used to take the original table name defined 
* / 
# query employee name, number types, types the name 
select e.last_name, e.job_id, j.job_title from employees as e, jobs as j where e.job_id = j.job_id ; 

. # 3 whether the order of the two tables can be swapped (can) 
# query employee names, jobs number, name trades
e.LAST_NAME the SELECT, e.job_id, j.job_title from Jobs AS J, where e.job_id the Employees AS E = j.job_id; 
# Case 2: Query There each department of the bonus department name and department heads number, and the minimum wage in the sector


. # 4 filter can do it 
# Case 1: Query the employees name bonuses, department name 
where e.commission_pct is not null # Filters 
use MyEmployees; 
the SELECT last_name, department_name, commission_pct from the Employees AS E, the Departments the WHERE AS E d. = d.department_id and e.commission_pct iS department_id not null; 

# case 2: query city name in the second character is o, the corresponding department name and city name 
select department_name, city from departments as d , locations as l where d.location_id l.location_id and l.city like = "_O%"; 

. # 5 can be added to the packet? 

Case # 1: the number of queries each city department of 
select count (*) as "number", city from departments as d, locations as l where d.location_id = l.location_id group by city;

min SELECT (the salary), DEPARTMENT_NAME, e.manager_id from the Employees AS E, D Departments AS WHERE IS e.department_id = Not e.commission_pct d.department_id and null by d.department_name Group, e.manager_id; 

. #. 6 sort can be added 

# 7 can achieve the three-table join it? 
Case #: inquiry staff name, department name and the city where 
the SELECT last_name, department_name, City from the Employees AS E, the Departments AS d, locations e.department_id the WHERE AS L = d.department_id and d.location_id = l.location_id; 



# 2. non equijoins 
# create a first table job_grades 
the CREATE tABLE job_grades 
(grade_level VARCHAR (. 3), 
 lowest_sal int, 
 highest_sal int); 

the INSERT the INTO job_grades 
the VALUES ( 'a', 1000, 2999); 

the INSERT the INTO job_grades 
the VALUES ( 'B', 3000, 5999); 
 
the INSERT the INTO job_grades
the VALUES ( 'C', 6000, 9999);
 
the INSERT the INTO job_grades 
the VALUES ( 'D', 10000, 14999); 

the INSERT the INTO job_grades 
the VALUES ( 'E', 15000, 24999); 

the INSERT the INTO job_grades 
the VALUES ( 'F.', 25000, 40,000); 
# View job_grades table 
use MyEmployees; 
the SELECT * from job_grades; 

# case 1: query wages and wage levels; 
the SELECT salary, grade_level from the employees AS E, job_grades AS JG the WHERE salary the BETWEEN lowest_sal and highest_sal; 
# add filters 
salary the SELECT, grade_level from the Employees AS E, job_grades the WHERE AS JG salary and the BETWEEN lowest_sal highest_sal and jg.grade_level = "A"; 


# 3 from connection (not quite understand, only know that a connection relationship in the same table). 

# case: queries employee name and the name of the parent 
select employee_id, last_name, manager_id from employees ;# Wrong 

# Case:
There are known table student id (student number), name, gradeID (year number)
Known grade table which has id (year number), name (Grade name) 
known table result there are id, score, studentNo (Student ID) 
requirements query name, grade names, grades 

select stu.name, gra.name, res .score from student as stu, grade as gra, result as res where stu.gradeID = gra.id and stu.id = res.studentNo;

  

Guess you like

Origin www.cnblogs.com/ivyharding/p/11543553.html