mysql-- multi-table - outer join query - connect the left and right connections, complex query conditions

create table employee ( num int(50),
                        d_id int(50),
                        name varchar(50),
                        age int(50),
                        sex varchar(50),
                        homeadd varchar(50)
                       );
insert into employee values(1,1001,'zhangsan ' , 26 is , ' NaN3 ' , ' Beijing ' ); INSERT INTO Employee values ( 2 , 1001 , ' Lisi ' , 24 , ' NV ' , ' hunan ' ); INSERT INTO Employee values ( . 3 , 1002 , ' wangwu ' , 25 , ' NaN3 ' ,'jiangsu'); insert into employee values(4,1004,'aric',15,'nan','yingguo');
select * from employee;




create table department ( d_id int(50), d_name varchar(50), functione varchar(50), address varchar(50) );
insert into department values(1001,'keyanbu','yanfachanpin','3lou5hao'); insert into department values(1002,'shengchanbu','shengchanchanp','5louyiceng'); insert into department values ( 1003 , ' xiaoshoubu ' , ' cehuaxiaoshou ' , ' 1louxiaoshoudating ' );

SELECT * from Department;




SELECT NUM, name, employee.d_id, Age, Sex, d_name, functione from Employee, Department WHERE employee.d_id = department.d_id ; inner join queries: query may be two or more tables, when the two tables have the same meaning as the field represented, the two tables may be connected through this field; when the value of this field, the query would out of the record. ================================================== ================================================== ================== Outer join queries: query may be two or more tables, an outer join queries need to be connected by the specified field. When the field value is equal to, you can check out the note. And the field is not equal to the value of the recording can also check out. External connections include: connect the left and right connection syntax: the SELECT list of attributes from table 1 left | right the Join table 2 ON table 1. 1 attribute name = Table name 2. Name 2 attribute; the list of attributes represents the fields to be queried name, these fields may be derived from different tables; left represents the left join query; rigth a right join query; ON condition of contact is connected to the rear; 1 , left connection query for the left join query, the query can be referred to the table 1 All records in the table. Indicated in the table name and table 2, only the records matching the query SELECT * from Employee; SELECT * from Department; SELECTNUM, name, employee.d_id, Age, Sex, d_name, functione from Employee left the Join Department ON employee.d_id = department.d_id; 2 , right connection query for a query when the right connection, a query can be referred to Table 2 Name all records. Table 1 and table names referred to only check out the matching records the SELECT * from the Employee; the SELECT * from Department; the SELECT NUM, name, employee.d_id, Age, Sex, d_name, functione from the Employee right the Join Department ON the Employee .d_id = department.d_id; complex query conditions in connection query, by adding additional constraints can make the results more accurate query SELECT * from employee; select * from department; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id and age > 24; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id Order by Age ASC ; SELECT statement to query connection manner out of the data from the employee and department tables. Then the query results in ascending order, sorted according to age field. ================================================== ================================================== =============================

 

Preparation table statement:

create table employee ( num int(50),
                        d_id int(50),
                        name varchar(50),
                        age int(50),
                        sex varchar(50),
                        homeadd varchar(50)
                       );
insert into employee values(1,1001,'zhangsan ' , 26 is , ' NaN3 ' , ' Beijing ' ); INSERT INTO Employee values ( 2 , 1001 , ' Lisi ' , 24 , ' NV ' , ' hunan ' ); INSERT INTO Employee values ( . 3 , 1002 , ' wangwu ' , 25 , ' NaN3 ' ,'jiangsu'); insert into employee values(4,1004,'aric',15,'nan','yingguo');
select * from employee;



create table department ( d_id int(50), d_name varchar(50), functione varchar(50), address varchar(50) );
insert into department values(1001,'keyanbu','yanfachanpin','3lou5hao'); insert into department values(1002,'shengchanbu','shengchanchanp','5louyiceng'); insert into department values(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating');
select * from department;

select * from employee;

 

 select * from department;

 

 

Left join query:

grammar:

select list of attributes from table 1 left | right join table 1. table 2 ON = 1 name attribute property name table 2. 2;

Property list represents the name of the field you want to query, these fields can come from different tables;

Left for left join query;

rigth for right join query;

Followed by contact condition on connection;


1, the left join query

When left join query, you can check out all the records referred to in Table 1 of the table name. And Table 2 referred to in the table name, you can only check out the matching records

select num,name,employee.d_id,age,sex,d_name,functione from employee left join department on employee.d_id = department.d_id;

 

 

 

 

 

2、右连接查询

进行右连接查询时,可以查询出表名2所指的表中的所有记录。而表名1所指的表中,只能查询出匹配的记录

select num,name,employee.d_id,age,sex,d_name,functione from employee right join department on employee.d_id = department.d_id;

 

 

 

 

 

 

3、复合条件查询

在连接查询时,通过增加其他的限制条件,可以使查询结果更加准确

 

select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id;

select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id and age > 24;

select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id order by age asc;

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12093071.html