-- 将wu的薪水在原有基础上增加1000元
update 表名 set sal=sal+1000 where name=’wu’;
-- in 查询数学分数为89,90,91的同学
select * from student where math in (89,90,91);
-- 分组group by统计身高,年龄平均和总年龄 (group by默认升序对分组字段排序,字典排序,且依赖校对集) 分组字段有几个不同值,就会分几组
select sex,count(*),max(height),min(height),avg(age),sum(age) from my_student group by 字段名 [desc(降序)];
-- 排序order by:对总分排序后再按升序(降序)输出 多字段排序:在order by后面加多个字段名及其排序方式
select math+english+chinese as allfen , name from student order by allfen [desc];
-- 分组计数:查询每个课程号和相应的选课人数
select 课程号, count(课程号) from 课表 group by 课程号;
-- 求和sum:数学,英语总成绩
select sum(math), sum(english) from student;
-- 求平均分avg:语文成绩平均分
select avg(math) from student; -- (NULL值不参与计算)
select sum(chinese)/count(*) from student;
-- 求最高分max(最低min):
select max(math) from student;
-- 对订单表中商品归类group by后,显示每一类商品的总价
select product, sum(price) from orders group by product;
-- having条件 在分组统计和起别名后使用
-- 对订单表中商品归类后,显示大于总价100的商品
select product, sum(price) from orders group by product having sum(price)>100;
-- between 闭区间[]
-- 查询年龄18-20岁的学生学号;
select sno from student where sage >= 18 and sage <= 20;
select sno from student where sage between 18 and 20;
-- n)查询总成绩大于200分的学生学号
select sno, sum(grade) from sc group by sno having sum(grade) > 200 ;
-- p)查询不及格课程超过3门的学生学号
select sno from sc where grade < 60 group by sno having count(*) > 3;
-- 分页查询limit,查询第2至4名学生
select * from student limit 1, 2;
-- 子查询:数学所有系学生成绩改成0 查阅:MySQL笔记.docx
update sc set grade = 0 where sno in (select sno sdept from student where sdept = 'ma');
-- 创建集合表
create table my_set(
hobby set('篮球','足球','乒乓球','羽毛球','排球','台球','网球','棒球')
-- 足球 台球 网球
-- 集合中: 每一个元素都是对应一个二进制位,被选中(插入)为1,没有则为0: 最后从右至左读取
-- 0 1 0 0 0 1 1 0 ←
-- 反过来 01100010 = 98
)charset utf8;
-- 插入数据
insert into my_set values('足球,台球,网球');
insert into my_set values(3);
-- 查看集合数据
select hobby + 0, hobby from my_set;
-- 98转成二进制 = 64 + 32 + 2 = 01100010
-- 颠倒元素出现的顺序
insert into my_set values('网球,台球,足球');
MySQL命令操作案例实例
最新推荐文章于 2024-08-06 15:20:26 发布