建表
create table stu(id int primary auto_increment,name
varchar(23),age int, grade_id int);
插入数据
insert into stu(name,age) values("tom", 20),("bob", 20),("lucy", 39);
查询
select * from stu name=tom
age between 20 and 30
age in (20, 30)
name like "t%" and age > 20
select * from stu limit 30 拿三十个
limit 2 offset 3 第三后拿两个
limit 3, 2
order by age 通过年纪升序列
age desc 降序
查询1班和2班每班有多少人,并按班级顺序排序
select class,count(*) from A where class in(1,2) group by class order by class;
分组
select avg(age) from stu group by grade_id;
select avg(age) from stu group by grade_id having avg(age) > 20;
链表查询
select * from t1 left join t2 on t1.t2_id=t2.id where t1.id>10
先查b表再查b表得出的结果
select * from (select * from b) as tmp;
查询所有 name 在p_name里面的
select * from a where name in (select p_name from person);
表明叫table_name的name的字段里
1.新建一个表
create table prize(id int primary key auto_increment,s_name varchar(30));
2.将数据插入新建的表中
insert into prize(s_nmae)select name as s_name from stu where grade>70;
insert table table_nmae(name) select p_name from person;
case when then
select case grade when 60 then 'A' when 90 then 'B' else '不及格' end from stu;
select case when grade > 60 then '及格' else '不及格' end as '级别' from stu;
select case when grade >= 90 then 'A' when grade < 90 and grade > 60 then 'B' else 'C' end as levels from stu;