单表基础

创建两个表

CREATE TABLE student (
id int(11) DEFAULT NULL COMMENT ‘学号’,
name varchar(20) DEFAULT NULL COMMENT ‘学生名字’,
score double DEFAULT NULL COMMENT ‘学生成绩’,
groupname varchar(20) DEFAULT NULL COMMENT ‘班级’

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE user (
name varchar(100) DEFAULT NULL,
pwd varchar(100) DEFAULT NULL,
id int(11) NOT NULL AUTO_INCREMENT,
age int(11) DEFAULT NULL,
sex varchar(1) DEFAULT NULL,
email varchar(255) DEFAULT NULL,
mobile varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

sql语句练习
use java9;
– *表示所有的列
select * from user;
– 如果查询结果不需要全部的列,可以把需要的列名放在select后面
select name,pwd from user;

– where是一个关键字,表示后面接查询的过滤条件
select * from user where age>10;
– 修改的格式: update 表名 set 列的名字=值
update user set sex=‘男’;

update user set sex=‘女’ where name=‘yyy’;
– 如果需要修改多列的数据,可以set后面用,隔开
update user set sex=‘女’,age=23 where name=‘天天’;

– 如果mobile是null值就不算了,只统计了mobile非空的多少条
select count(mobile) from user;

– count()的话,所有列只要有一列有值,就算一条
select count(
) from user where sex=‘男’;
– avg算列的平均值
select avg(age) from user;
– as 表示取别名
select count() as ‘男生总数’ from user where sex=‘男’;
– group by 分组
– 先分组, 再计数
select sex,count(
) from user group by sex;

select * from student;
– 统计每个班的人数
select groupname, count() from student group by groupname;
– 统计每个班的及格人数
select groupname, count(
) from student where score>=60 group by groupname;

– 统计每个班,大于80分的人数,并且降序排列
select groupname, count() from student where score>=80 group by groupname order by count() desc;

– 统计每个班的及格人数,并且只显示及格人数大于6的记录
select groupname, count() from student where score>=60 group by groupname having count()>6;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值