MYSQL学习DAY1笔记

一、MYSQL中查询五子句

select * from 数据表       ① where子句
                        ② group by 分组子句
                        ③ having子句
                        ④ order by子句
                        ⑤ limit子句

注意事项:①②③④⑤五子句是SQL中最重要的五个子句,而且其顺序不能颠倒

二、查询语句

select * from tb_student;				
select id,name,address from tb_student;

1、回顾where子句

(1)等于 = ; 不等于 !=,<>

# 查询tb_student表中姓名为‘貂蝉’的同学的所有信息
select * from tb_student where name = '貂蝉';
# 查询tb_student表中姓名不为‘貂蝉’的同学的所有信息
select * from tb_student where name != '貂蝉';
select * from tb_student where name <> '貂蝉';
# 查询tb_student表中年龄大于20岁的同学信息
select * from tb_student where age > 20;

(2)查询结合与and、或or、非not

# 查询年龄为34岁且性别为male的同学信息
select * from tb_student where age = 34 and gender = 'male';
# 查询id编号为1或3或5的同学信息
select * from tb_student where id = 1 or id = 3 or id = 5;
# 查询id编号不为1或3或5的同学信息
select * from tb_student where id not in(1,3,5);

(3)where子句与like模糊查询,like模糊查询有两个符合 %百分号 和 _下划线

# %代表任意个任意字符
# _代表任意的某个字符(只能表示一个字符)
# 查询班级中姓“孙”的同学信息
select * from tb_student where name like '孙%';
# 查询班级中姓'刘'且姓名为两位数的同学的信息
select * from tb_student where name like '孙_';
# 查询班级中以'婵'字结尾的同学信息
select * from tb_student where name like '%婵';
# 查询班级中包含'乔'字的同学信息
select * from tb_student where name like '%乔%';

(4)空值判断(NULL)

# 查询age年龄为NULL的同学信息
select * from tb_student where age is null;
# 查询age年龄不为NULL的同学信息
select * from tb_student where age is not null;

2、order by 子句,(排序子句,升序12345、降序54321)

# order by 不仅可以对数字进行排序,还可以对字母进行排序
# (升序12345,降序54321)
# order by 字段名称 asc
# order by 字段名称 desc
# 查询班级中所有同学信息,按年龄从小到大排序
select * from tb_student order by age asc;
# 注-在排序时,null一般被认为是最小值
# 多个字段进行排序,先排order最左侧的字段,当字段值相同时,则继续按右侧的字段进行排序(了解)
select * from tb_student order by age asc,height asc;
​

3、limit子句(五子句的最后一个)

# 只要最高的
select * from tb_student order by age desc limit 1;
# 要第二第三
select * from tb_student order by age desc limit 1,2;

三、常见的聚合函数

1. count(col): 表示求指定列的总行数
2. max(col): 表示求指定列的最大值
3. min(col): 表示求指定列的最小值
4. sum(col): 表示求指定列的和
5. avg(col): 表示求指定列的平均值

# 求班级中所有同学的数量
select count(id) from tb_student; 
# 求班级中年龄最大值
select max(age) from tb_student;
# 求班级中年龄最小值
select min(age) from tb_student;
# 求班级中所有年龄总和
select sum(age) from tb_student;
# 求班级中年龄平均值
select avg(age) from tb_student;

# 扩展 对null处理的函数ifnull(列名,默认值),如果某个列的值为null,则系统绘自动将其设置为右边的默认值
# 求班级中所有同学的平均值
select avg(age) from tb_student;
select avg(ifnull(age,0) from tb_student;

四、分组函数

# group by 分组子句:顾名思义,要把分析的数据通过某种形式进行分组,如:按学科、性别、车次、部门等
# 为什么要进行分组?为了更好地进行数据地统计操作(group by结合5个聚合函数 )
select * from tb_student group by gender;------------错误

# > 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_itheima.tb_student.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

select x from tb_student group by x;

# 统计班级中男女同学的比例(男同学多少人,女同学多少人)
select count(id) from tb_student where gender = 'male';
select count(id) from tb_student where gender = 'female';

select gender, count(*) from tb_student group by gender;

# select从数据表中读取数据就是一个循环遍历的过程,首先读取第一组数据,然后读取第二条数据,以此类推--直至数据遍历结束
# 求每个学科中,年龄最大的
select subject,max(age) from tb_student group by subject;

# having子句,对查询的结果进行过滤和筛选
# 普通查询中,having子句可以替代where子句(含义相同)
select * from tb_student where age > 20;
select * from tb_student having age > 20;

# 在有group by分组子句中,having和where有很大的不同
# 对分组后的结果进行过滤和筛选

# 根据gender字段进行分组,统计分组条数大于2条以上的分组信息
select gender,count(id) from tb_student group by gender having count(id) > 2;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值