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

时间:2023-03-09 18:13:20
潭州课堂25班:Ph201805201 MySQL第三课 (课堂笔记)

单表查询:

  select * from

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

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

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

select sname from stu;

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

条件查询

select sname from stu where sid=2;

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

select sname from stu where sid>2;

select sname from stu where sid!=2;

查询时取别名,

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

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

模糊查询,

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

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

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

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

排序    order by  字段名

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

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

select * from stu order by tzid desc;        降

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

限制查询  limit

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

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

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

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

平均值:

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

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

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

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

统计: 有几条数据

select count(age) from dstu;

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

求最大值

> select MIN(age) from dstu;

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

求最小值

select  MIN(age) from dstu;

求和

select  SUM(age)  from  dstu;

分级查询GROOP  BY

统计出现的次数,

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

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

统计每个科目报名人数,

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

   科目   学生人数        学生表         科目

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

分组条件查询,having

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

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

多表查询  (着想查询)

select * from dstu where age > 19.5;

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

子查询:

  一条语句结合两条语句,

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

着想查询:

  内连接 [INNER| CROSS] JOIN

同时查询二个表,

select * from tanzhou,stu;

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

select * from stu inner join tanzhou;

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

关联查询:

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

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

外连接 { LEFT| RIGHT } JOIN

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

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

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

作业;:

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

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

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

在此基础上查改名,..

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

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

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

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

找出分数在70到90的。

补充