数据库
首页 > 数据库> > MySQL_2

MySQL_2

作者:互联网

1. 字段的别名

-- 通过as给字段起一个别名
select card as身份证,name as姓名,sex as 性别 from students;
-- 别名的as可以省略
select card身份证,name姓名,sex 性别 from students;

2. 表的别名

-- 通过as给表students起一个别名
select  from students as stu;
-- 可以省略as
select  from students stu;

3. distinct过滤重复记录

SELECT DISTINCT sex,class fr om students;

4.where子句

-- 例1:查询students表中学号studentNo 等于"001’的记录
select * from students where studentNo = '001";
-- 例2:查询students表中年龄age等于30的姓名name,班级class
select name,class from students where age = 30;

5.select查询的基本规律

6.比较运算符

-- 例1:查询students 表中name(姓名〉等于'小乔"学生的age(年龄)
select age from students where name = '小乔';
-- 例2:查询students表中30岁以下的学生记录
select from students where age < 30;
-- 例2:查询students表中30岁和30岁以下的学生记录
SELECT * from students where age <= 30;
--查询家乡不在'北京'的学生记录
select * from students where hometown !='北京';
select * from students where hometown <> '北京";

7.逻辑运算符

8.模糊查询

-- 例1:查询name姓名中以"孙"开头的学生记录
SELECT from students where name like '孙%';
-- 例2:查询name 姓名以'孙"开头,且名只有一个字的学生记录
SELECT * from students where name like '孙';
-- 例3:查询name为任意姓,名叫'乔”的学生记录
SELECT * from students where name like '%养乔';
-- 查询name姓名有"白’子的学生记录
SELECT 全 from students where name like '%白%';

9.范围查找

10.空判断

-- 例1:查询card身份证为nu1l的学生记录
SELECT R from students where card is nu11 ;
-- 例2:查询card身份证非nu11的学生记录
SELECT from students where card is not nu11;

11.where子句可以用到update和delete语句后面

-- 例1:修改age 为25,并且 name为'孙尚香"的学生class为”2班”
update students set class = '2班' where age = 25 and name = '孙尚香';

-- 例2:删除class为”1班”,并且age 大于30的学生记录
DELETE from students where class = '1班' and age > 30;

12.order by排序

-- 例1:查询所有学生记录,按age年龄从小到大排序
select * from students order by age asc;
select from students order by age;
-- 例2:查询所有学生记录,按age年龄从大到小排序
select from students order by age desc;
-- 例2:查询所有学生记录,按age年龄从大到小排序,
-- 年龄相同时,再按studentNo学号从小到大排序
SELECT from students ORDER BY age desc, studentNo;
-- 练习:查询所有男学生记录,按class班级从小到大排序,班级相同时,
-- 再按studentNo学号再按学号从大到小排序
SELECT * from students where sex = '男' order by class,studentNo desc;

13.聚合函数

where age = max(age) ----> 错误!!!

count求select返回的记录总数

-- 查询学生总数(查询stuents表有多少记录)
select count(*) from student s;
select count(name) from students;
select count (DISTINCT class) from students;
select count (DISTINCT sex) firom students;
-- 查询女同学数量
SELECT count(name) from students where sex = '女';
SELECT count(*) from students where sex ='女';
SELECT count(sex) from students where sex ='女';

max查询最大值

-- 查询students中的最大年龄
SELECT max(age) from students ;
-- 查询students中的女生最大年龄
SELECT max(age) from students where sex = '女';
-- 查询students中的'1班'最大年龄
SELECT max(age)from students where class = '1班';

min查询最小值

-- 查询students中的最小年龄
SELECT min(age) from students ;
-- 查询students中的女生最小年龄
SELECT min(age) from students where sex ='女';
-- 查询students中的1班最小年龄
SELECT min(age) from students where class = '1班';

sum求和

-- 查询students中的年龄总和
SELECT sus(age from students ;
-- 查询students中的女生年龄总和
SELECT sum(age) from students where sex ='女';
-- 查询students中的'1班'年龄总和
SELECT sum(age) from students where class = ‘1班';

avg求平均数

-- 查询students中的年龄总和
SELECT sus(age) from students ;
-- 查询students中的女生年龄总和
SELECT sum(age) from students where sex ='女';
-- 查询students中的'1班'年龄总和
SELECT sum(age)from students where class = '1班';

14.数据分组

-- 分别查询男女同学的数量
SELECT count(*)from students where sex ='男';
SELECT count(*)from students where sex='女';
select sex,count(i) from students group by sex;
-- 分别查询各个年龄段的同学数量
select age,count(*) from students group by age;
-- 分别查询'1班'不同性别学生数量
select sex,count(*) from students where class = '1班' group by sex;
-- 练习:统计各个班级学生总数、平均年龄、最大年龄、最小年龄。
-- 但不统计'3班',统计结果按斑级名称从大到小排序
SELECT class,count(*),avg(age),max(age), min(age) from studentswhere class '3班' GROUP BY class ORDER BY class desc;

order by是对整个结果进行排序,所以要放在最后

15.分组聚合之后的数据筛选

-- 用where查询男生总数
-- where先锦选复合条件的记录,然后在聚合统计
SELECT count(*) from students where sex ='男';
-- 用having查询男生总数
-- having先分组聚合统计,在统计的结果中饰选
SELECT count(*) from students GROUP BY sex HAVING sex = '男';

having配合聚合函数的使用

-- 求班级人数大于3人的班级名字
select class from students GROUP BY class HAVING count(*) >3;

16.having与where筛选的区别

17.limit显示指定的记录数

-- 查询前三行记录
SELECT  from students limit 0,3;
SELECT  from students limit 3;
-- 查询从第4条记录开始的三条记录
SELECT from students 1imit 3,3;
-- 查询年龄最大同学的name
select name from students 0RDER BY age desc limit 1;
-- 查询年龄最小的女同学信息
SELECT * from students where sex ='女' ORDER BY age LIMIT 1;

18.数据分页显示

-- 每页显示4条记录,第3页的结果
select from students limit 8,4;
-- 每页显示4条记录,第2页的结果
select  from students limit 4,4;

标签:students,age,--,SELECT,MySQL,where,select
来源: https://www.cnblogs.com/lzy5967/p/16652920.html