笔记总结

数据库书写要求
1.主键 :pk_...id
2.外键 :fk_...id
3.属性 :f_...
4.表名 :t_...
5.外键约束 :fk_表1_表2
语句书写
-- 查询语句  *表示所有值
SELECT * from t_student;


-- 查询时可以根据自身要求返回需要的值  select 列名 from 表名
-- 查询学生表中的名字
SELECT f_name from t_student;


-- 查询多个列,名字和成绩
SELECT f_name,f_grade from t_student;


-- 将查询的结果的列名改名称   在查询时可以使用as 后面加上别名
SELECT f_name as '名字',f_grade as '成绩' from t_student;


-- 在书写别名时,可以省略as
SELECT f_name '姓名1',f_grade '成绩' from t_student;


-- 根据表名来提示列名  在投影位置使用表名.列名的方式
SELECT t_student.f_name '姓名' from t_student;


-- 别名不仅可以使用在属性上,还可以使用在表名上  多表连接时
SELECT s.f_name '姓名' from t_student s;


-- 条件where
-- 查询年龄>18岁的人的姓名
SELECT f_name '姓名' from t_student where f_age>18;


-- 查询成绩在60-90(包含)之间的学生信息  and表示并且
SELECT * from t_student where f_grade>=60 and f_grade<=90;


-- 查询成绩为60分或者男生的学生信息
SELECT * from t_student where f_grade=60 or f_gender='男';


-- 区间条件 between 小值 and 大值    比较的属性是同一个  和and有关
SELECT * from t_student where f_grade BETWEEN 60 and 90;


-- 条件 in(条件1,条件2,....)  比较的属性是同一个  和or相关
SELECT * from t_student where f_age in(18,19);
-- f_age in(18,19)相当于  f_age=18 or f_age=19


-- 返回指定多少行数据limit
-- 查询表中5行数据
SELECT * from t_student LIMIT 5;


-- limit也可以指定从哪儿开始找几个  limit 参数1,参数2   参数1是从第几个开始  参数2是找几个
-- 注意查询结果的值在limit中参数1从0开始计算
-- 成绩在90分以上的第二个人
SELECT * from t_student where f_grade>90 limit 1,5;


-- 模糊查询 like(像)  在模糊查询中,模糊查询也就是给的条件是不准确的
-- =是准确查找  like '模糊条件'  _指代一个任意字符   %零个或多个任意字符
SELECT * from t_student where f_name like '张__';


-- 排序 默认为升序
SELECT *  from t_student ORDER BY f_age;
-- 根据年龄降序desc
SELECT *  from t_student ORDER BY f_age DESC;
-- 根据年龄升序asc
SELECT *  from t_student ORDER BY f_age ASC;
-- 根据年龄降序,年龄相同时根据成绩升序(升序默认可以不写)
SELECT * from t_student ORDER BY f_age desc,f_grade;


-- 根据年龄降序排序男生信息
SELECT * from t_student where f_gender='男' ORDER BY f_age desc;


-- 找出男生年龄最大的人的信息
SELECT * from t_student where f_gender='男' ORDER BY f_age desc limit 1;




-- 查询时做数据进行处理,不会更改原数据
SELECT f_grade,f_name,f_age + 10 '年龄',f_gender '性别' from t_student where f_age BETWEEN 18 and 25;


-- count() 返回结果的行数目,但是在count(列)如果该列有null值,则不计入数目
-- count(*)即使某一列值为null也不无所谓,也会计入数目
SELECT count(*) from t_student;
SELECT f_name from t_student;




-- sum()计算某列数据相加总和--注意需要传入的是可以加和的列
SELECT sum(f_age) from t_student;


-- avg()计算某列数据的平均值
SELECT avg(f_age) from t_student;


-- 补充,精确小数round()四舍五入


-- 将本班的年龄的平均数保留2位小数    
-- round()精确到个位  round(值,num) 值为要精确的数值,num为精确到几位
-- num位正数,则为保留几位小数,而负数,则为保留个位,十位,百位这种操作
SELECT round(avg(f_age),2) from t_student;


-- max()求最大值
SELECT max(f_age) '最老' from t_student;


-- min()求最小值
SELECT min(f_grade) '最老' from t_student;


-- 查询出没有名字的人的信息
SELECT f_name '姓名',f_grade '成绩' from t_student where f_name is null;


-- 将没有名字的人的名字设置为小日月
UPDATE t_student set f_name='小日月' where f_name is null;


-- concat()拼接
SELECT CONCAT(f_name,'-',f_grade) '姓名-成绩' from t_student;




-- 分组
SELECT * from t_student;


SELECT 
f_gender '性别',count(f_gender) '人数',
sum(f_age) '总年龄',round(avg(f_age),2) '平均年龄'
from t_student GROUP BY f_gender;




SELECT f_age,count(f_age) from t_student GROUP BY f_age;


-- 查询男女生中,哪个群体的平均年龄>20岁
-- 注意:在where中不能将聚合函数作为判断条件
-- 注意:如果在分组的情况下,想使用聚合函数作为比较条件,则需要使用having
-- 在SQL中where和having的区别:1.where不能使用聚合函数,having可以
-- 2.where的执行速度在分组前,也就是说where不受分组限制,having只能在分组后使用
SELECT avg(f_age) '平均年龄',f_gender '性别' from t_student 


GROUP BY f_gender HAVING avg(f_age)>20;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值