多表查询-外连接
REATE TABLE dept( id INT PRIMARY KEY auto_increment, name VARCHAR(20) ); INSERT INTO dept (NAME) VALUE ('开发部'),('市场部'),('财务部'); CREATE TABLE emp( id INT PRIMARY KEY auto_increment, name VARCHAR(10), gender CHAR(1), salary DOUBLE, join_date date, dept_id INT, FOREIGN KEY (dept_id) REFERENCES dept(id) ); insert into emp(name,gender,salary,join_date,dept_id) VALUES ('孙悟空','男',7200,'2013-02-24',1); insert into emp(name,gender,salary,join_date,dept_id) VALUES ('猪八戒','男',3600,'2010-12-02',2); insert into emp(name,gender,salary,join_date,dept_id) VALUES ('唐僧','男',9000,'2008-08-08',2); insert into emp(name,gender,salary,join_date,dept_id) VALUES ('白骨精','女',5000,'2015-10-07',3); insert into emp(name,gender,salary,join_date,dept_id) VALUES ('蜘蛛精','女',4500,'2011-03-14',1); -- 创建表格
左外连接:
语法:select 字段列表 from 表1 left [outer] join 表2 on 条件
查询的事左表所有数据以及其交集部分
SELECT t1.*,t2.name from emp t1 left join dept t2 on t1.dept_id = t2.id
右外连接:
语法:select 字段列表 from 表1 right [outer] join 表2 on 条件
查询的事右边表所有数据以及其交集部分
SELECT t1.*,t2.name from emp t1 right join dept t2 on t1.dept_id = t2.id
子查询概
概念:查询中嵌套查询,称嵌套查询为子查询。
查询工资最高的员工信息
-- 查询最高的工资是多少 SELECT MAX(salary) FROM emp; -- 查询员工信息,并且工资等于9000的 SELECT * FROM emp WHERE emp.salary = 9000; -- 结合(子查询) SELECT * FROM emp WHERE emp.salary = (SELECT MAX(salary) FROM emp);
标签:salary,多表,查询,524,dept,emp,join,id From: https://www.cnblogs.com/agzq/p/16783868.html