Mysql自学笔记三

1.创建表

create table student(

stid int auto_increment primary key,

sname varchar(10) not null,

sex enum('男','女','保密'),

dept varchar(20) not null default '计算机科学与技术',

score float

)auto_increment=1 charset=utf8;

2.查看表结构

desc student;

show create table student;

3.修改表名

alter table student rename to st1;

4.修改列名

alter table student change tid sid int;

5.修改列类型和长度

alter table student modify sname char(30);

6.在dept列之后增加一列

alter table student add city varchar(10) after dept;

7.删除列

alter table student drop city;

8.插入数据

insert into student values(1,'李磊','男','美术',89.0);

insert into student(sid,sname,score) values(2,'韩磊',88.2);

9.插入数据,如果存在此数据则用心数据进行替换,如果没有则直接插入

replace into student(sid,sname,score) values(2,'咪咪',89.2);

10.修改数据

update student  set sex='女' where sid=3;

11.删除数据

delete from student where sid=2;

12.为查询字段起别名

select sid as '学号', sname as '姓名',dept as '系',score as '成绩' from student;

13.查询年纪大于等于10的学生

select  * from student where age>=10;

14.查询年纪大于等于9的学生

select  * from student where age=9;

15.查询年纪在9到10之间的学生

select * from student where age between 9 and 10;

16.查询年纪不在9到10之间的学生

select * from student where age not between 9 and 10;

17.查询注册日期在2016年7月15日-2016年7月30日之间的学生

select * from student where date between '2016-7-15' and '2016-7-30';

18.查询姓名是2个字的学生

select * from student where sname like '____';

select * from student where char_length(sname)=4;

19.查询出姓李的学生

select * from student where sname like '李%';

20查询注册日期是2016年7月15日和2016年7月27日的学生

select * from student where date in ('2016-7-15 0:0:0','2016-7-27 0:0:0');

select * from student where date='2016-7-15 0:0:0' or date='2016-7-27 0:0:0';

21.查询注册日期不是2016年7月15日和2016年7月27日的学生

select * from student where date not in ('2016-7-15 0:0:0','2016-7-27 0:0:0');

select * from student where date!='2016-7-15 0:0:0' and date!='2016-7-27 0:0:0';

22.查询年纪为空的学生

select * from student where age is null;

23.将年龄为空的学生的年龄统一设置为9岁

update student set age=9 where age is null;

24.跨数据库查表

select * from mysql.event;
25.只显示3条数据(limit)

select * from student limit 3;
26.只显示从第2条起的2条数据

select * from student limit 1,2;

27.查询所有学生按年龄从大到小排序

select * from student order by age desc;
28.查询所有学生按年龄从小到大排序

select * from student order by age;
29.聚合函数(不允许直接出现在where语句中;主要配合分组语句 group by)
   avg()-求平均数(值为null不参与平均分计算)
   count()-统计个数
   sum()-求和
   max()-求最大值
   min()-求最小值
30.查询出最大年龄

select max(age) from student;
31.查询出最大年龄的学生信息

select * from student where age =(

select max(age) from student

);
32.统计学生人数

select count(*) from student;
33.统计9岁学生人数

select count(*) from student where age=9;
34.查询没有填写生日的学生人数

select count(*) from student where date is null;
35.统计不同学科的人数

select dept as '学科', count(*) as '人数' from student group by dept;
36.分组(group by)情况下,select中只能出现聚合函数和分组的字段
37.统计不同学科的人数,并按人数从大到小排序

select dept as '学科', count(*) as '人数' from student group by dept order by count(*) desc;
38.统计不同学科的人数,并按人数从大到小排序,年级为空的不参与统计

select dept as '学科', count(*) as '人数' from student where age is not null group by dept order by count(*) desc;

39.统计不同学科的人数,并按人数从大到小排序,年龄为空的不参与统计,且学科人数小于1的不显示
select dept,count(*) from student where age is not null group by dept having count(*)>1 order by count(*) desc;

40.各学科的最高分
  select dept as '学科',max(score) as '分数' from student group by dept;
41.求出上海年龄最大的学生

select * from student where age=(
select max(age) from student where city='上海')

and city='上海';

42.求出北京年龄最大的学生

select * from student where age=(
select max(age) from student where city='北京')

and city='北京';


43.求出上海和北京年龄最大的学生

select * from student where age=(
select max(age) from student where city='上海')

and city='上海'

union--取合集,字段的类型和数目应该一致

select * from student where age=(
select max(age) from student where city='北京')

and city='北京';


44.多表查询--公司不推荐使用
 select student.sid,teacher.tid
  from student,teacher
  where student.tid=teacher.tid;
45.连接查询--jion 、left jion、 right jion
 
46.--自连接查询出员工的编号、姓名及领导的名字
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值