赞
踩
navicat初学者笔记(4)
#1、连接查询 SELECT name,boyName FROM beauty,boys where beauty.boyfriend_id = boys.id; #2、自链接:查询员工名的领导名(把同一个表用两种格式命名) SELECT e.last_name,e.employee_id,m.last_name,m.employee_id FROM employees e,employees m WHERE e.manager_id = m.employee_id #3、sql99语法 join on 例: SELECT e.last_name,e.employee_id,m.last_name,m.employee_id FROM employees e inner JOIN employees m ON e.manager_id = m.employee_id #4、外连接:查询一个表有,另一个表没有的数据LEFT JOIN 左边是主表 SELECT name ,bo.* FROM beauty b LEFT OUTER JOIN boys bo ON b.boyfriend_id = bo.id; #5、全外连接,不分主从表,查询两个表格的并集 SELECT name ,bo.* FROM beauty b FULL OUTER JOIN boys bo ON b.boyfriend_id = bo.id;#编译器不支持 SELECT name ,bo.* FROM beauty b FULL OUTER JOIN boys bo ON b.boyfriend_id = bo.id where b.boyfriend_id= NULL;#编译器不支持 #6、子查询 /*分类:1、SELECT(支持标量子查询)、FROM(支持表子查询) 、WHERE、HAVING(支持标量、列、行子查询)、EXISTS(支持表子查询) 2、标量子查询(结果只有一行一列)、列子查询(结果集只有一列多行)、表子查询(多行多列) */ #7、标量子查询查询工资比Abel高的员工信息 SELECT * FROM employees WHERE salary > ( SELECT salary FROM employees WHERE last_name = 'Abel' ); #8、列子查询 IN ANY|SOME ALL 例:查询工资大于部门编号为100的任意员工工资 SELECT * FROM employees WHERE salary > ANY( SELECT salary FROM employees WHERE department_id = 100 ); #9、行子查询 例:查询编号最小、工资最高的员工信息 SELECT * FROM employees WHERE (employee_id,salary) IN ( SELECT MIN(employee_id),MAX(salary) FROM employees ); #10、select 后面的子查询 例:查询每个部门的员工数 SELECT d.*, ( SELECT COUNT(*) FROM employees e WHERE d.department_id = e.department_id ) FROM departments d; #11、FROM 后面的子查询,把查询结果当成一张表,必须起别名 #12、exists后面的子查询(相关子查询) /*语法: exists(完整查询语句) 结果:返回0或1,表示是否存在 */ #例:查询有员工的部门名 SELECT d.department_name FROM departments d WHERE EXISTS (SELECT * FROM employees e inner JOIN departments ON d.department_id = e.department_id ) ; #13、分页查询(应用场景:一页显示不全,分页显示) SELECT * FROM employees LIMIT 5; SELECT * FROM employees LIMIT 10,15; #14、联合查询(把一个大但查询条件拆分),可以同时查询多个表不需要相互关联。 SELECT b.id ,name FROM beauty b UNION SELECT bo.id,bo.boyName FROM boys bo;#结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。