码迷,mamicode.com
首页 > 其他好文 > 详细

黑马1

时间:2017-07-27 18:19:41      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:uid   表达式   模糊查询   ssid   不能   UI   表名   --   表达   

--------------聚合函数的一些问题------------

--聚合函数不统计空值

select avg (age) from student

select sum(age) from student

--如果使用聚合函数的时候,没有手动group by ,那么聚合函数会把整个表中的数据当做一组来统计

--------------------带条件查询------------------

select 列 from 表名 where 条件

--查询没有及格的学生(假设:数学或者英语,只要有一门没有及格的就叫做没有及格)的学号

select * from student select stuID from student where english <60 or math<60

--查询年龄在20-30之间的男学生

select * from student  where age >=20 and age <=30 and sex=‘男‘

--查询数学成绩在80-90之间的学生

select * from  student where math between 80 and 90

--查询所有班级ID为3,4,5的学生

select * from student where classID=3 or classID=4 or classID =5

select * from student where classID in (3,4,5)

--对于in或者or查询,如果查询中的条件是连续的几个数字,最好使用>= ,<=

select * from student where classID>=3 and classID< =5

-----模糊查询-----------(针对字符串)

--通配符:_    %    []   ^

--_表示任意的一个字符

--两个字的

select * from student where name like‘张_‘

--三个字的 select * from student where name like‘张__‘

--无论姓名几个数字,只要第一个数字是张的就查询出来

select * from student where name like‘张%‘ and 

--[]表示筛选,范围。

select * from student where name like ‘张[0-9]妹‘

select * from student where name like ‘张_妹‘

select * from student where name like ‘张[a-z]妹‘

select * from student where name like ‘张[a-z0-9]妹‘

--两者不同 not限制了张和妹

select * from student where name like ‘张[^0-9]妹‘

select * from student where name not like ‘张[0-9]妹‘

--只查询中间带百分号的人

select * from student where name like ‘%[%]%‘

--[]默认转义符   escape ‘/‘ 指定转义符

select * from student where name like ‘%/[%      escape ‘/‘

------------空值处理-------------

--查询所有年龄为空的同学信息

--null值无法用=或者<>来进行比较

--判断null值必须用is null 或者is not null

select * from student where name is null

--查询所有年龄不为null的同学信息

select * from studnet where name is not null

-----------通过order by 语句进行排序-----------

--1.降序order by 列名 desc

--2.升序 order by 列名 或者 order by 列名 asc

--3.order by 语句必须一定要放在整个sql语句的最后面。

select * from biao inner join... where... group by... having... order by ...

--4.根据多列进行排序 --先根据英语成绩排序,在根据数学成绩排序(指的是英语成绩相同的时候 再按照数学成绩排序)

select * from student order by english desc ,math desc

--5.可以根据表达式排序

select * from student order by (english+math)/2 desc

--请从学生表中车讯出每个班级的班级ID和班级人数

select   classID as ‘班级ID‘,  

  班级人数=count(*)  

  from student

   group by classID

--请统计出,所有中男同学与女同学的人数分别是多少

select  

    性别=‘sex‘,

   人数=count(*)

   from student group by sex

--请从学生表中车讯出每个班级的班级ID和班级中男同学人数

select

   classID as ‘班级ID‘,

    男同学人数=count(*)

   from student

  where sex=‘男‘ group by classID

--当使用了分别语句(group by)或者是聚合函数的时候,在select的不能含有其他的列,除非该列也出现在了group by或者聚合函数中。

--对分组以后的数据进行筛选使用having

--having和where都是对数据进行筛选,where是对分组前的每一行书记进行筛选,having是对分组后的数据进行筛选。

select  

  classID as ‘班级ID‘,  

  班级人数=count(*)  

   from student

  group by classID  

  having count(*)>10 order by 班级人数 asc

--执行顺序

1.from

2.on

3.join

4.where

5.group by

6with cube或者with rollup

7.having

8.select

9.disinct

10.order by

11.top

黑马1

标签:uid   表达式   模糊查询   ssid   不能   UI   表名   --   表达   

原文地址:http://www.cnblogs.com/wanghonglin/p/7245710.html

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