用一条SQL 语句 查询出每门课都大于80 分的学生姓名
方法1:
1、查询出分数小于80的人的名字;
2、查询 NOT IN (1);
方法2:
select name from table group by name having min(fenshu)>80
删除掉重复项只保留一条
delete from a where id in (
select id from (
select id from a where expect in
(select expect from a group by expect having count(expect)>1)
and id not in (select min(id) from a group by expect having count(expect)>1)
)
)
删除除了自动编号不同, 其他都相同的学生冗余信息
自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
delete from table where 自动编号 not in
(select min(自动编号) from table group by 其他字段)
两张关联表,删除主表中已经在副表中没有的信息
Delete from a where not exists (select * from b where a.infid=b.infid )
一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合.
select a.name,b.name from
team a,team b
where a.name<b.name
复制表结构及数据到新表/只复制表结构到新表
CREATE TABLE 新表 SELECT * FROM 旧表
CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2
拷贝表( 拷贝数据, 源表名:a目标表名:b)
insert into b(a, b, c) select d,e,f from a;
横纵表转化
a:纵表
b:横表
纵变横:
select name,
sum(case subjects when '语文' then score else 0 end) as 'yuwen',
sum(case subjects when '数学' then score else 0 end) as 'shuxue',
sum(case subjects when '英语' then score else 0 end) as 'yingyu'
from a group by name;
横变纵:
SELECT name,'语文' AS subjects,yuwen AS score FROM b UNION ALL
SELECT name,'数学' AS subjects,shuxue AS score FROM b UNION ALL
SELECT name,'英语' AS subjects,yingyu AS score FROM b
ORDER BY name,subjects DESC;
查询平均成绩大于60分的同学的学号和平均成绩
select stuId,avg(score)
from Scores
group by stuId having avg(score) >60;
select stu_id ,avg(score)
from a
group bu stu_id having avg(score)>60;
有两个表A 和B ,均有key 和value 两个字段,如果B 的key 在A 中也有,就把B 的value 换为A 中对应的value
update b set b.value = (select a.value from a where a.key = b.key)
where b.id in (select b.id from b,a where b.key = a.key)