MySQL查年龄18到22的信息_MySQL之单表查询

1.查询所有老师的信息

select*fromteacher;

2.在没有表被引用的情况下,允许使用dual作为一个假的表名

selectnow()fromdual;select1+1fromdual;

selecta+bfromdual;--错误的,因为得不到具体的结果

3.查询ID为9的老师信息

select*fromteacherwheret_id=9;

4.查询职业为null/不为null的老师的信息

select*fromteacherwheret_jobisnull;

select*fromteacherwheret_jobisnotnull;

5.使用别名查询ID为9的老师的姓名和性别

selectt_name姓名,t_sex性别fromteacherwheret_id=9;

6.查询性别为女和指定日期的老师信息 (and 表示条件同时满足)

select*fromteacherwheret_sex='女'andbirthday='2011-11-11';

7.查询ID大于5的老师信息

select*fromteacherwheret_id>5;

8.查询性别为男或者ID小于9的老师信息(or 表示条件满足其一即可)

select*fromteacherwheret_sex='男'ort_id<9;

9.查询姓名最后一个字符为d的老师信息

select*fromteacherwheret_namelike'%d';

10.查询姓名以指定字符开头的老师信息

select*fromteacherwheret_namelike'冰%';

11.查询姓名中包含'士'的老师信息

select*fromteacherwheret_namelike'%士%';

查询姓名最后一个字符为d,且只有两个字符的老师信息.

select*fromteacherwheret_namelike'_d';

查询姓名最后一个字符为d,且只有三个字符的老师信息.

select*fromteacherwheret_namelike'__d';

注: (1)%匹配任意个字符(零个或者多个) (2)_匹配一个字符

12.查询所有老师信息,并按日期降序/升序排列 (排序关键字order by, desc降序,asc升序可以省略.)

select*fromteacherorderbybirthdaydesc;

select*fromteacherorderbybirthdayasc;

select*fromteacherorderbybirthday;

13.多个排序条件:先按t_age升序排序,再按birthday降序排序

select*fromteacherorderbyt_ageasc,birthdaydesc;

14.按性别分组分别查询男女老师的人数

selectt_sex,count(t_sex),avg(t_age)fromteachergroupbyt_sex;

注:分组关键字group by,有分组的sql语句中,查询的字段只能是被分组的字段和聚合函数.

15.按性别分组,只查询出女老师的人数 (having 对分组后的数据进行过滤)

selectt_sex,count(t_sex)fromteachergroupbyt_sexhavingt_sex='女';

16.查询年龄在20到30岁之间的老师信息(between and 表示一个范围)

select*fromteacherwheret_age>20andt_age<30;

select*fromteacherwheret_agebetween20and30;

17.查询老师信息的前三条

select*fromteacherlimit0,3;--查询第一条到第三条记录

select*fromteacherlimit2,3;--查询第三条到第五条记录select*fromteacherlimit4;--如果开始位置是0,可以省略.

注:limit限制查询的条数, 第一个参数是开始位置,从0开始. 第二个参数是要查询的条数

18.查询年龄是22,25,28岁的老师信息 (in 关键字,表示一个范围)

select*fromteacherwheret_age=22ort_age=25ort_age=28;

select*fromteacherwheret_agein(22,25,28);

19.查看一共有多少个老师

selectcount(*)fromteacher;

20.查询老师的平均年龄/最大年龄/最小年龄/年龄总和

selectavg(t_age)平均年龄,max(t_age)最大年龄,min(t_age)最小年龄,sum(t_age)年龄总和fromteacher;

SQL语句中常用的运算符:

算数运算符:

+,-,*,/,%取余

比较运算符:

>,=,<=,!=,<>不等于,

in,betweenand,isnull,isnotnull,like等.

逻辑运算符:

not,and,or

sql语句中where , group by, having ,order by ,limit 的顺序:

where-->groupby-->having-->orderby-->limit

select选择的列from表名 where查询的条件groupby分组属性having分组过滤的条件orderby排序属性asc/desclimit起始记录位置,记录的条数

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值