码迷,mamicode.com
首页 > 数据库 > 详细

SQL 复习二(数据查询语言)

时间:2016-10-23 21:03:50      阅读:424      评论:0      收藏:0      [点我收藏+]

标签:2.7   复习   group by   条件查询   分组查询   类型   ping   相加   部门   

1.1 数据查询语言

DQL就是数据查询语言,数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端。

语法:

SELECT selection_list /*要查询的列名称*/

  FROM table_list /*要查询的表名称*/

  WHERE condition /*行条件*/

  GROUP BY grouping_columns /*对结果分组*/

  HAVING condition /*分组后的行条件*/

  ORDER BY sorting_columns /*对结果分组*/

  LIMIT offset_start, row_count /*结果限定*/

1.基础查询

1.1 查询所有列

select * from stu;

1.2 查询指定列

select sid,sname form stu;

 

2.条件查询

2.1 条件查询介绍

条件查询就是在查询时给出where字句,在where字句中限定条件:

=,!=,<>,<,<=,>,>=;

beteween……and……

in(set)

is null;

and;

or;

not;

 

2.2 查询性别为女,并且年龄50的记录 

select * from stu where gender=‘female’ and age>50;

2.3 查询学号为S_1001,或者姓名为liSi的记录

slect * from stu where sid=s_1001’ or name=‘liSi’;

2.4 查询学号为S_1001,S_1002,S_1003的记录

select * from stu where sid in(s_1001,s_1002,s_1003); 

2.5 查询学号不是S_1001,S_1002,S_1003的记录

select * from stu where sid not in(s_1001,s_1002,s_1003);

2.6 查询年龄为null的记录

select * from stu where age is null;

2.7 查询年龄在20到40之间的学生记录

select * from stu where age>20 and age<40;
或者
select * from stu where age between 20 and 40;

2.8 查询性别非男的学生记录

select * from stu where gender!=male;
或者
select * from stu where gender<>male;
或者
select * from stu where not gender=‘male’;

2.9 查询姓名不为null的学生记录

select * from stu where sname is not null;
或者
select * from stu where not sname is null;

 

3.模糊查询

模糊查询需要用到关键字like。

3.1 查询姓名由5个字母构成的学生记录

select * from stu where sname like‘_____’;

模糊查询必须使用LIKE关键字。其中 “_”匹配任意一个字母,5个“_”表示5个任意字母。

 

3.2 查询姓名由5个字母构成,并且第5个字母为“i”的学生记录

select * from stu where sname like____i;

3.3 查询姓名以“z”开头的学生记录

select * from student where name like z%;

其中“%”匹配0~n个任何字母。

 

3.4 查询姓名中第2个字母为“i”的学生记录

select * from stu where sname like_i%;

3.5 查询姓名中包含“a”字母的学生记录

select * from stu where sname like %a%;

 

4 字段控制查询

4.1 去除重复记录 (用distinct 关键字)

select distinct sal from emp;

4.2 查看雇员的月薪与佣金之和

  因为sal和comm两列的类型都是数值类型,所以可以做加运算。如果sal或comm中有一个字段不是数值类型,那么会出错。

select ename,sal+comm from emp;

comm列有很多记录的值为NULL,因为任何东西与NULL相加结果还是NULL,所以结算结果可能会出现NULL。下面使用了把NULL转换成数值0的函数IFNULL:

select ename,sal+ifnull(comm,0) from emp;

4.3 给列名添加别名

在上面查询中出现列名为sal+IFNULL(comm,0),这很不美观,现在我们给这一列给出一个别名,为total:

select ename , sal+ifnull(comm,0) as total from emp;
或者
select ename , sal+ifnull(comm,0) total from emp;

给列起别名时,是可以省略AS关键字的: 

 

5 排序

5.1 查询所有学生记录,按年龄升序排序

select * from stu where order by age asc;
或者 
select * from stu where order by age ;

 默认情况按升序排序。 

5.2 查询所有学生记录,按年龄降序排序

select * from stu where order by age desc

5.3 查询所有雇员,按月薪降序排序,如果月薪相同时,按编号升序排序

select * from emp order by sal desc,empno asc
select * from emp order by sal desc,empno asc

 

 6 聚合函数

 聚合函数是用来做纵向运算的函数:

count():统计指定列不为null的记录行数;

 max():计算指定列的最大值,

min():计算指定列的最小值。

sum():计算指定列的数值和,如果指定列类型不是数值,那么计算结果为0;

avg():计算指定列的平均值,如果指定列的类型不是数值,那么计算结果为0;

 

6.1 COUNT

当需要纵向统计时可以使用COUNT()。

      查询emp表中记录数:

select count(*)  as cnt from emp;
  •    查询emp表中有佣金的人数:
select count(comm) as cnt from emp;

注意,因为count()函数中给出的是comm列,那么只统计comm列非NULL的行数。

 

  • 查询emp表中月薪大于2500的人数:
select count(*) as cnt from emp where sal>2500;
  •    统计月薪与佣金之和大于2500元的人数:
select count(*)  as cnt from emp where sal+ifnull(comm,0)>2500;
  •    查询有佣金的人数,以及有领导的人数:
select count(comm),count(mgr) from emp;

6.2 SUM和AVG

当需要纵向求和时使用sum()函数。

  •   查询所有雇员月薪和:
select sum(sal) from emp;
  •  查询所有雇员月薪和,以及所有雇员佣金和:
select sum(sal),sum(comm) from emp;
  •   查询所有雇员月薪+佣金和:
select sum(sal+ifnull(comm,0)) from emp;
  •   统计所有员工平均工资:
select avg(sal) from emp;
或者
select sum(sal)/count(sal) from emp;

6.3 MAX和MIN

  •  查询最高工资和最低工资:
select max(sal),min(sal) from emp;

 

7 分组查询

 当需要分组查询时,需要使用group by 子句,例如查询每个部门的工资和。

7.1 分组查询

  •  查询每个部门的部门编号和每个部门的工资和:
select deptno,sum(sal) from emp group by deptno;
  •   查询每个部门的部门编号以及每个部门的人数:
select deptno,count(*) from emp group by deptno;
  •  查询每个部门的部门编号以及每个部门工资大于1500的人数:
select deptno,count(*) from emp where sal>1500 group by deptno;

7.2 HAVING子句

  • 查询工资总和大于9000的部门编号以及工资和:
select deptno,sum(sal) from emp group by deptno having sum(sal)>9000; 

注意,WHERE是对分组前记录的条件,如果某行记录没有满足WHERE子句的条件,那么这行记录不会参加分组;而HAVING是对分组后数据的约束。

 8 LIMIT

LIMIT用来限定查询结果的起始行,以及总行数。

8.1 查询5行记录,起始行从0开始

select * from emp limit 0,5;

注意,起始行从0开始,即第一行开始!

8.2 查询10行记录,起始行从3开始

select * from emp limit 3,10;

 

 

 

SQL 复习二(数据查询语言)

标签:2.7   复习   group by   条件查询   分组查询   类型   ping   相加   部门   

原文地址:http://www.cnblogs.com/yuqt/p/5984201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!