潭州课堂25班:Ph201805201 MySQL第三课 (课堂笔记)

 

 单表查询:

  select * from

 

 

 select sname from stu;

 

 

条件查询

select sname from stu where sid=2;

select sname from stu where sid>2;

select sname from stu where sid!=2;

 

 

 查询时取别名,

select sid as 学号,sname as 姓名 from stu;

 

 模糊查询,

select * from stu where sname like '小%';    %  有多少相似查出多少,

select * from stu where sname like '小_';      _  有几条线,查出几条,,

select * from stu where sname like '小___';      _  有几条线,查出几条,,

 

 

排序    order by  字段名

select * from stu order by tzid 【asc】;    升

 

select * from stu order by tzid desc;        降

 

限制查询  limit

select * from stu limit 3;         可以理解为第 0 条开始往下 3条,

 

 

select * from stu limit 3,1    从第 3 条往下第 1 第,

 

平均值:

select avg(age) from dstu;      求培元年龄,

 

sql> select round(avg(age)) from dstu;        四舍五入平均值,统计

 

 

 统计: 有几条数据

select count(age) from dstu;

 

求最大值 

> select MIN(age) from dstu;

 

求最小值

select  MIN(age) from dstu;

 

求和

select  SUM(age)  from  dstu;

 

 

分级查询GROOP  BY

统计出现的次数,

select count(*) from stu GROUP BY tzid;      可以看成是查询每个科目的报名人数,

 

统计每个科目报名人数,

select tzid,count(sid) from stu group by tzid;

   科目   学生人数        学生表         科目

 

 

 分组条件查询,having

select tzid as 科目,count(sid) as 学生人数 from stu group by tzid having 学生人数=2;

 

 

 

多表查询  (着想查询)

select * from dstu where age > 19.5;

子查询:

  一条语句结合两条语句,

select * from dstu where age > (select avg(age) from dstu);

 

 

 

着想查询:

  内连接 [INNER| CROSS] JOIN

同时查询二个表,

select * from tanzhou,stu;

 

 

select * from stu inner join tanzhou;

 

 

关联查询:

  select * from stu inner join tanzhou on tzid=sid;

 

 

外连接 { LEFT| RIGHT } JOIN

select * from stu left  join dstu on id = sid; 

 

与内连接相比,可以显示所有学生,包括未选课的学生,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

作业;:

查询学生详情表性别为男,并年龄大于18,

select * from dstu where age>18 and sex='b';

 

 

在此基础上查改名,..

 

 需求: 作为宿管,想知道学生的 ( 姓名, 年龄,性别,所属学

 

 

 

select  name js from 表单名 where js between 70 and 90 ;

找出分数在70到90的。

 

 

 

 

 

补充

 

转载于:https://www.cnblogs.com/gdwz922/p/9256686.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值