// 基本语法 [distinct 去重]select[DISTINCT]*|{列名...} from 表名
// 使用表达式对查询的列进行运算select*|{列名...|表达式...} from 表名
// 使用as语句select 列名 as 别名 from 表名
// 使用order by 进行排序,位于select末尾select*from 表名 orderby 列名或别名 asc|desc.....
使用where进行过滤操作
比较运算符
作用
> < <= >= = <> !=
大于、小于…
between … and …
在某一区间内
in (set)
显示在in列表中的值
%like% ‘…’ not like ‘…’
模糊查询 %模糊匹配
is null
判断是否为空
逻辑运算符
作用
and
与
or
或者
not
不成立
mysql表查询-加强
# 增强查询// 使用where子句-- q:如何查找时间在1992.1.1后入职的员工select*from emp where hiredate >date('1992-01-01');// 使用like操作符// %:表示0到多个字符 _:表示单个字符-- q:如何显示首字符为s的员工姓名和工资select ename,sal from emp where ename like'S%';-- q:如何显示第三个字符为大写o的所有员工姓名和工资select ename,sal from emp where ename like'__O%';// 如何显示没有上级的雇员的情况select*from emp where mgr isnull;// 查询表结构desc 表名;// 使用order by子句-- q:如何按照工资的从低到高的顺序,显示雇员的信息select*from emp orderby sal;-- (默认升序,desc为降序)-- q:按照部门号升序而雇员的工资降序排列,显示雇员信息select*from emp orderby deptno, sal desc;
select case when expr1 then expr2 when expr3 then expr4 else expr5 end;
如果expr1为TRUE,则返回expr2,如果expr2为t,返回expr4,否则返回expr5
多表查询
#多表查询-- q:显示雇员名,雇员工资以及所在部门的名字[笛卡尔集]select ename,sal,dname from emp,dept where emp.deptno = dept.deptno;-- q:如何显示部门号为10的部门名、员工名和工资select dname,ename,sal from emp,dept where emp.deptno = dept.deptno and emp.deptno =10;-- q:显示各个员工的姓名,工资,及其工资的级别select ename,sal,grade from emp,salgrade where emp.sal >= salgrade.losal and emp.sal <= salgrade.hisal;
#自连接-- q:显示公司员工的名字和他的上级名字// 给表取别名,as 可以省略select a.ename,b.ename from emp a,emp b where a.mgr = b.empno;
#多行子查询// 子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询// 单行子查询-- q:如何显示与SMITH同一部门的所有员工select*from emp where deptno =(select deptno from emp where ename ='SMITH');// 多行子查询// 多行子查询指返回多行数据的子查询 使用关键字in-- q:如何查询和部门10的工作相同的雇员的名字、岗位、工资、部门号,但是不含10自己的。select ename,job,sal,deptno from emp
where job in(selectdistinct job from emp where deptno =10)and deptno !=10;-- 可能会去重复,用distinct
#子查询临时表// 查询ecshop中各个类别中,价格最高的商品-- 先查询一张临时表select cat_id,Max(shop_price)from ecs_goods GROUPBY cat_id
-- 再使用临时表和总表关联查询出最后结果select goods_id,temp.cat_id,goods_name,ecs_goods.shop_price from
ecs_goods,(select cat_id,Max(shop_price) max_sal from
ecs_goods GROUPBY cat_id)tempwheretemp.max_sal = ecs_goods.shop_price;// all和any-- q:显示工资比部门30的所有员工的工资高的员工的姓名,工资和部门号select ename,sal,deptno from emp where sal >(selectMax(sal)from emp where deptno =30);select ename,sal,deptno from emp where sal >all(select sal from emp where deptno =30);-- q:如何显示工资比部门30的其中一个员工的工资高的员工的姓名、工资和部门号select ename,sal,deptno from emp where sal >(selectMin(sal)from emp where deptno =30);select ename,sal,deptno from emp where sal >any(select sal from emp where deptno =30);
#多列子查询// 多列子查询则是返回多个列数据的子查询-- q:查询和宋江数学,英语,语文完全相同的学生(一一对应)select*from students where(math,english,chinese)=(select math,english,chinese from students where name ='宋江');-- q:查询与smith的部门和岗位完全相同的所有雇员(并且不含smith本人)select*from emp where(job,deptno)=(select job,deptno from emp where ename ='SMITH')and ename <>'SMITH';
练习
#练习-- 查找每个部门工资高于本部门平均工资的人的资料select emp.*from
emp,(select deptno,avg(sal) avg_sal from emp groupby deptno) t where
emp.deptno = t.deptno and emp.sal > avg_sal;-- 查找每个部门工资最高的人的详细资料select emp.*from emp,(select deptno,Max(sal) max_sal from
emp groupby deptno) t where
emp.deptno = t.deptno and emp.sal = t.max_sal;--显示每个部门的信息(包括部门名字,编号,地址)和人员数量select dept.deptno,dname,loc,total_num from
dept,(select deptno,count(*) total_num from emp groupby deptno)tempwheretemp.deptno = dept.deptno;
合并查询
#合并查询// union 和 union all-- union all 不会去重select ename,sal,job from emp where sal >2500select ename,sal,job from emp where job ='MANAGER'select ename,sal,job from emp where sal >2500unionallselect ename,sal,job from emp where job ='MANAGER'-- union 会去重select ename,sal,job from emp where sal >2500unionselect ename,sal,job from emp where job ='MANAGER'