Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
alter table user add age int unsigned not null default 0 comment '年龄' after gender;
update user set age= timestampdiff(year,borthday,now());
update user set name='xiaowang' where name='wang';
delete from user where id=1;
查询数据库
select database()
查询版本
select vsersion()
查询当前时间
select now();
select * from user where id>950;
select name as 姓名,count(1) as数量 from user group by name;
select name 姓名,count(1) 数量 from user group by name;
select * from user where name='li' order by id desc;
![](https://img.php.cn/upload/image/669/941/590/1616663725280251.png)
从2到3
select from user order by age desc limit 2 offset 1;![](https://img.php.cn/upload/image/163/911/787/1616663736497138.png)
年龄最大
select from user where age=(select max(age) from user);`
select * from user where id>=10 and id <=20;
select * from user where id between 10 and 20;
select * from user where id in (1,3,5);
select * from user where name like 'z%';
select * from user where name like '%a%';
select * from user where name like '%g';
select gender ,count(*) from user group by gender;
select gender ,avg(age) 平均年龄 from user group by gender;
select gender ,count(*) from user group by gender having gender='male';
select a.id,a.title,b.name from articles a, cates b where a.cid=b.cid;
select id,title,name from articles a inner join cates b on a.cid=b.cid;
//简化
select id,title,name from articles a join cates b using(cid);
//过滤
select id,title,name from articles a inner join cates b on a.cid=b.cid where a.cid=1;
select * from articles a left join cates b on a.cid=b.cid;
select * from articles a right join cates b on a.cid=b.cid;
select * from articles a left join cates b on a.cid=b.cid where a.cid is not null;
select id,title,name from articles natural join cates;
create view v_user as select * from user;
create view vc_user as select name,age from user;
select * from vc_user;
update vc_user set age =40 where name='wang';
查询索引
//普通索引
create index age on user(age);
//唯一索引
create unique index unique_age on user(age);
//主键索引
alter table user add primary key id(id);
show index from user;