mysql将一个表的数据导入到另一个表

将一个表的数据插入到另外一个表中的几种情况如下:

1.如果2张表的字段一致,并且希望插入全部数据,可以用这种方法:
INSERT INTO 目标表 SELECT * FROM 来源表;
例如:

insert into insertTest select * from insertTest2;

2.如果只希望导入指定字段,可以用这种方法:
INSERT INTO 目标表 (字段1, 字段2, …) SELECT 字段1, 字段2,… FROM 来源表;(这里的话字段必须保持一致)
例如:

insert into insertTest2(id,name) select id,name from insertTest2;

注意:如果目标表与来源表主键值相同则会出现添加错误,主键值不同才能插入

3.如果您需要只导入目标表中不存在的记录,可以使用这种方法:
INSERT INTO 目标表 (字段1, 字段2, …) SELECT 字段1, 字段2, … FROM 来源表
WHERE not exists (select * from 目标表 where 目标表.比较字段  = 来源表.比较字段);
例如:
1>.插入多条记录:

       insert into insertTest2(id,name) select id,name from insertTest
where not exists (select * from insertTest2 where insertTest2.id = insertTest.id);

2>.插入一条记录:

       insert into insertTest (id, name) SELECT 100,’liudehua’  FROM dual
WHERE not exists (select * from insertTest where insertTest.id = 100);

4、如果需要导入的目标表字段比来源表的字段多,将来源表的数据导入再加上几个字段组成目标表   的数据
INSERT INTO 目标表 (目标字段1,目标字段2,字段1, 字段2,…)
select 目标字段1,目标字段2, 字段1, 字段2,…  FROM来源表
目标字段1,目标字段2:这是目标表比来源表多出的字段
例如:

    insert into insertTest2(目标字段1,目标字段2,id,name) select 目标字段1,
目标字段2, id,name from insertTest  where insertTest2.id = insertTest.id;
目标字段1,目标字段2:可以先设占位符,在设置值。也可以直接在语句中赋值
insert into insertTest2(目标字段1,目标字段2,id,name) select  a1,
a2, id,name from insertTest  where insertTest2.id=insertTest.id;

5、对上述4的解析

 select a1, a2, id,name from insertTest

这里在表 insertTest里本身没有a1,a2两个字段名,当使用这个查询语句时,会查 出 insertTest表的所有字段值,并在表数据的前面加上了列名为a1,a2的字段,并且列名为a1的值全为a1,列名为a2的值全为a2,并且a1,a2不能为变量,如果是变量,sql语句会把它当做表字段,而表中不存在这个字段,会报错

总结:即可以向一个表中查询不存在的列名,这里不存在的列名必须是实际值或占位符,不能是变量

MySQL报错:sql_mode=only_full_group_by 4种解决方法含举例

首先,打开数据库,输入

select @@global.sql_mode;

这个时候,就会返回得到以下的信息:(不同电脑返回的信息可能不同)

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION

如果里面包含 ONLY_FULL_GROUP_BY,那么就重新设置,在数据库中输入以下代码,去掉ONLY_FULL_GROUP_BY即可:

SET GLOBALsql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';

 

MySQL根据某一个或者多个字段查找重复数据的sql语句

MySql 删除多个字段重复的数据(只保留一条)

 DELETE FROM 表名 WHERE (字段1,字段2,字段3)
IN 
(SELECT 字段1,字段2,字段3 FROM (SELECT 字段1,字段2,字段3 FROM 表名 GROUP BY 字段1,字段2,字段3 HAVING COUNT(*)>1) s1) 
AND
id NOT IN (SELECT id FROM (SELECT id FROM 表名 GROUP BY 字段1,字段2,字段3 HAVING COUNT(*)>1) s2);

sql 查出一张表中重复的所有记录数据

1.表中有id和name 两个字段,查询出name重复的所有数据

select * from xi a where (a.username) in (select username from xi group by username having count(*) > 1)

 

2、查询出所有数据进行分组之后,和重复数据的重复次数的查询数据,先列下:

1
select count(username) as '重复次数',username from xi group by username having count(*)>1 order by username desc

3、一下为 查看别人的 结果,现列下:查询及删除重复记录的方法大全

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

1
2
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

1
2
3
delete from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)

3、查找表中多余的重复记录(多个字段)

1
2
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

1
2
3
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

1
2
3
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

(二)

比方说

在A表中存在一个字段“name”,

而且不同记录之间的“name”值有可能会相同,

现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;

1
Select Name,Count(*) From A Group By Name Having Count(*) > 1

如果还查性别也相同大则如下:

1
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

(三)

方法一

1
2
3
4
5
6
7
8
9
10
11
declare @max integer,@id integer
declare cur_rows cursor local for
select 主字段,count(*) from 表名 group by 主字段 having count(*) >;
open cur_rows fetch cur_rows into @id,@maxwhile @@fetch_status=0
begin
 select @max = @max -1
 set rowcount @max
 delete from 表名 where 主字段 = @id
fetch cur_rows into @id,@maxend
close cur_rows
set rowcount 0

方法二"重复记录"有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。

1、对于第一种重复,比较容易解决,使用

1
select distinct * from tableName

就可以得到无重复记录的结果集。

如果该表需要删除重复的记录(重复记录保留1条),

可以按以下方法删除

1
2
3
4
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp

发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。

2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下  假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集

1
2
3
select identity(int,1,1) as autoID, * into #Tmp from tableName
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID
select * from #Tmp where autoID in(select autoID from #tmp2) 

最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)

(四)查询重复

1
select * from tablename where id in (select id from tablenamegroup by idhaving count(id) > 1)

对一个字段查找重复记录

根据sample_code字段找到重复记录

1
SELECT * FROM tb_table WHERE sample_code IN( SELECT sample_code FROM tb_table GROUP BY sample_code HAVING COUNT(sample_code) > 1 );

对多个字段查找重复记录(这里以2个为例)

根据name和code字段找到重复记录

1
2
3
4
SELECT * from (SELECT *, CONCAT(name,code) as nameAndCode from tb_table) t WHERE t.nameAndCode in
(
 SELECT nameAndCode from (SELECT CONCAT(name,code) as nameAndCode from tb_table) tt GROUP BY nameAndCode HAVING count(nameAndCode) > 1
)

总结

以上所述是小编给大家介绍的MySQL根据某一个或者多个字段查找重复数据的sql语句,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

复杂sql语句练习(mysql)

下面是练习表数据和结构

SET FOREIGN_KEY_CHECKS=0;

— —————————-
— Table structure for course
— —————————-
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`id` int(11) NOT NULL,
`name` varchar(22) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

— —————————-
— Records of course
— —————————-
INSERT INTO `course` VALUES (‘30001’, ‘物理’);
INSERT INTO `course` VALUES (‘30002’, ‘政治’);
INSERT INTO `course` VALUES (‘30003’, ‘语文’);
INSERT INTO `course` VALUES (‘30004’, ‘高数’);
INSERT INTO `course` VALUES (‘30005’, ‘英语’);

— —————————-
— Table structure for student
— —————————-
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(22) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

— —————————-
— Records of student
— —————————-
INSERT INTO `student` VALUES (‘10001’, ‘tom’);
INSERT INTO `student` VALUES (‘10002’, ‘json’);
INSERT INTO `student` VALUES (‘10003’, ‘ak’);
INSERT INTO `student` VALUES (‘10004’, ‘km’);
INSERT INTO `student` VALUES (‘10005’, ‘mk’);

— —————————-
— Table structure for student_course
— —————————-
DROP TABLE IF EXISTS `student_course`;
CREATE TABLE `student_course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`teacher_id` int(11) NOT NULL,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8;

— —————————-
— Records of student_course
— —————————-
INSERT INTO `student_course` VALUES (‘4’, ‘10001’, ‘30002’, ‘20002’, ’82’);
INSERT INTO `student_course` VALUES (‘6’, ‘10001’, ‘30003’, ‘20003’, ’82’);
INSERT INTO `student_course` VALUES (‘8’, ‘10001’, ‘30004’, ‘20005’, ’82’);
INSERT INTO `student_course` VALUES (’10’, ‘10001’, ‘30005’, ‘20005’, ’82’);
INSERT INTO `student_course` VALUES (’12’, ‘10002’, ‘30001’, ‘20001’, ’90’);
INSERT INTO `student_course` VALUES (’14’, ‘10002’, ‘30002’, ‘20002’, ’92’);
INSERT INTO `student_course` VALUES (’16’, ‘10002’, ‘30003’, ‘20003’, ’62’);
INSERT INTO `student_course` VALUES (’18’, ‘10002’, ‘30004’, ‘20004’, ’82’);
INSERT INTO `student_course` VALUES (’20’, ‘10002’, ‘30005’, ‘20005’, ’82’);
INSERT INTO `student_course` VALUES (’22’, ‘10003’, ‘30001’, ‘20001’, ’69’);
INSERT INTO `student_course` VALUES (’24’, ‘10003’, ‘30002’, ‘20002’, ’89’);
INSERT INTO `student_course` VALUES (’26’, ‘10003’, ‘30003’, ‘20003’, ’99’);
INSERT INTO `student_course` VALUES (’28’, ‘10003’, ‘30004’, ‘20004’, ’82’);
INSERT INTO `student_course` VALUES (’30’, ‘10003’, ‘30005’, ‘20005’, ’82’);
INSERT INTO `student_course` VALUES (’32’, ‘10004’, ‘30001’, ‘20001’, ’92’);
INSERT INTO `student_course` VALUES (’34’, ‘10004’, ‘30002’, ‘20002’, ’93’);
INSERT INTO `student_course` VALUES (’36’, ‘10004’, ‘30003’, ‘20003’, ’73’);
INSERT INTO `student_course` VALUES (’38’, ‘10004’, ‘30004’, ‘20004’, ’82’);
INSERT INTO `student_course` VALUES (’40’, ‘10004’, ‘30005’, ‘20005’, ’82’);
INSERT INTO `student_course` VALUES (’42’, ‘10005’, ‘30001’, ‘20001’, ’95’);
INSERT INTO `student_course` VALUES (’44’, ‘10005’, ‘30002’, ‘20002’, ’75’);
INSERT INTO `student_course` VALUES (’46’, ‘10005’, ‘30003’, ‘20003’, ’79’);
INSERT INTO `student_course` VALUES (’48’, ‘10005’, ‘30004’, ‘20004’, ’82’);
INSERT INTO `student_course` VALUES (’50’, ‘10005’, ‘30005’, ‘20005’, ’82’);

— —————————-
— Table structure for teacher
— —————————-
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL,
`name` varchar(22) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

— —————————-
— Records of teacher
— —————————-
INSERT INTO `teacher` VALUES (‘20001’, ‘su’);
INSERT INTO `teacher` VALUES (‘20002’, ‘wang’);
INSERT INTO `teacher` VALUES (‘20003’, ‘zhou’);
INSERT INTO `teacher` VALUES (‘20004’, ‘yang’);
INSERT INTO `teacher` VALUES (‘20005’, ‘liu’);

 

4个表,包括student,course,student_course,teacher

详细练习:

— 1.查询物理成绩比英语成绩高的所有学生
select * from
(select * from student_course WHERE course_id=30001) a,
(select * from student_course WHERE course_id=30005) b
where a.score>b.score and a.student_id=b.student_id;

— 2.查询平均成绩大于60分的同学的学号和平均成绩;
select a.student_id,avg(a.score) from
student_course a
GROUP BY a.student_id
having avg(a.score)>60 ;

— 3查询所有同学的学号,姓名、选课数、总成绩;
select b.student_id,a.name,COUNT(b.course_id),SUM(b.score)
from student a ,student_course b
where a.id=b.student_id
GROUP BY b.student_id ;

— 4、查询名字l开头的老师的个数;
select COUNT(DISTINCT(name)) FROM teacher where name like ‘l%’;

— — 5,查询没学过“liu”老师课的同学的学号、姓名;
select * from student where id not in(
select DISTINCT(student_id) from student_course where teacher_id=(
select DISTINCT(id) from teacher where name=”liu”)
);

— 6 学过30001和30002课程的同学的学号、姓名;–
select a.student_id,b.name
from student_course a,student b
where a.student_id=b.id and a.course_id=30001 and exists(
select* from student_course s2 where s2.student_id=a.student_id and s2.course_id=30002
)
group by a.student_id;

— 7 学过30001和30002课程的同学的学号
select a.student_id from
(select * from student_course where course_id=30001) a inner join
(select * from student_course where course_id=30002) b
on a.student_id=b.student_id;

— 8 、查询学过“liu”老师所教的课的所有同学的学号、姓名;

select * from student where id in
(
select distinct(student_id) from student_course where teacher_id=
(
select distinct(id) from teacher where name=”liu”
)
);

— 9查询课程编号“30002”的成绩比课程编号“30001”课程低的所有同学的学号、姓名;
select*from student where id in(
select distinct(a.student_id) from
(select * from student_course where course_id=30002) a inner join (select * from student_course where course_id=30001) b
on a.score<b.score)
;

— 10 查询没有学全所有课的同学的学号、姓名
select a.student_id,b.name
from student_course a,student b
where a.student_id=b.id
group by a.student_id
having count(a.course_id) <> (select count(*) from course);

— 11查询所有课程成绩小于90分的同学的学号、姓名;
select * from student where id not in (
select student_id from student_course where score>90
);

— 12查询至少学过学号为“10001”同学所有一门课的其他同学学号和姓名
select *from student where id in (
SELECT distinct(student_id) FROM student_course where student_id <> 10001 and course_id in
(
SELECT course_id FROM student_course where student_id=10001
)
);

select distinct a.id,a.name from student a, student_course b where a.id=b.student_id and b.student_id <> 10001 and b.course_id in(
SELECT course_id FROM student_course where student_id=10001
);

— 13把“student_course”表中“liu”老师教的课的成绩都更改为此课程的平均成绩
update student_course set score =(
select score1 from (
select avg(b.score) as score1 from teacher a,student_course b where a.name=”liu” and b.teacher_id=a.id
)
as tablename2
)where teacher_id=(select id from teacher where name=”liu”);

select * from student_course;
update student_course set score=82.1667 where teacher_id=20004;
SET SQL_SAFE_UPDATES = 0;
show variables like ‘SQL_SAFE_UPDATES’;

— 14查询和“10002”号的同学学习的课程完全相同的其他同学学号和姓名;

select b.id,b.name from student_course a,student b
where a.course_id in(
select course_id from student_course where student_id=10002
) and a.student_id=b.id and a.student_id <> 10002
group by b.id
having count(*)=(select count(*) from student_course where student_id=10002);

— 15 删除学习“su”老师课的student_course表记录;
delete from student_course
where teacher_id=(
select id from teacher where name=”su”
);

— 16查找要求符合以下条件:没有上过编号“30003”课程的同学学号、
— “30003”号课的平均成绩;
select id,(select avg(score) from student_course where course_id=”30003″) from student where id not in(
select student_id from student_course where course_id=”30003″
);

— 17按平均成绩从高到低显示所有学生的“物理”、“政治”、“英语”三门的课程成绩,
— 按如下形式显示: 学生ID,物理,政治,英语,有效课程数,有效平均分
select t.student_id as 学生ID,
(select score from student_course where t.student_id=student_id and course_id=”30001″ ) as 物理,
(select score from student_course where t.student_id=student_id and course_id=”30002″ ) as 政治,
(select score from student_course where t.student_id=student_id and course_id=”30005″ ) as 英语,
count(*) AS 有效课程数, AVG(t.score) AS 平均成绩
from student_course t
group by t.student_id
order by avg(t.score);

— 18 查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select course_id as 课程ID,max(score) as 最高分,min(score) as 最低分
from student_course
group by course_id;

— 19 按各科平均成绩从低到高和及格率
select course_id as 课程ID,avg(score) as 平均成绩,
100*sum(case when score>=60 then 1 else 0 end) /count(*) as 及格百分数
from student_course t
group by course_id
order by avg(score) desc;

— 20.查询如下课程平均成绩和及格率的百分数(用”1行”显示):
— 物理(30001),政治(30002),语文 (30003),高数(30004)

select
(select avg(score) from student_course where course_id=”30001″) as 物理,
(select avg(score) from student_course where course_id=”30002″) as 政治,
(select avg(score) from student_course where course_id=”30003″) as 语文,
(select avg(score) from student_course where course_id=”30004″) as 高数,
100*sum(case when score>=60 then 1 else 0 end)/count(*) as 及格率,
avg (score) as 平均成绩
from student_course
group by course_id;

— 21 查询不同老师所教不同课程平均分从高到低显示
select avg(score)
from student_course
group by teacher_id
order by avg(score) desc;

— 22统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
select a.course_id as 课程ID,
b.name as 课程名称,
sum(case when a.score between 85 and 100 then 1 else 0 end) as “[100-85]”,
sum(case when a.score between 70 and 85 then 1 else 0 end) as “[85-70]”,
sum(case when a.score between 65 and 70 then 1 else 0 end) as “[70-60]”,
sum(case when a.score <60 then 1 else 0 end) as “[<60]”
from student_course a,course b
where a.course_id=b.id
group by a.course_id,b.name;

— 23 查询学生平均成绩
select a.student_id as 学生ID,b.name as 姓名,avg(a.score) as 平均成绩
from student_course a,student b
where a.student_id=b.id
group by a.student_id
order by avg(a.score) desc;

— 24 查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT t1.student_id as 学生ID,
t1.course_id as 课程ID,
t1.score as 分数
FROM student_course t1
WHERE t1.id IN (
select t.id from(
SELECT id
FROM student_course
ORDER BY score DESC
limit 3) as t
)
ORDER BY t1.score desc;

— 25查询每门课程被选修的学生数
select count(student_id)
from student_course
group by course_id;

— 26 查询出只选修了一门课程的全部学生的学号和姓名
select a.student_id,b.name
from student_course a,student b
where b.id=a.student_id
group by student_id
having count(course_id)=1;

— 27 查询姓“t”的学生名单
select * from student where name like “t%” ;

— 28 查询同名同性学生名单,并统计同名人数
select name ,count(name) from student group by name;

— 29 查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select avg(score) ,course_id from student_course group by course_id order by avg(score),course_id desc ;

— 30 查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select a.student_id,b.name,avg(a.score)
from student_course a,student b
where a.student_id=b.id
group by a.student_id
having avg(a.score)>85;

— 31 查询课程名称为“政治”,且分数低于60的学生姓名和分数
select a.student_id,a.score,b.name
from student_course a,student b
where a.student_id=b.id and a.course_id=(
select id from course where name =”政治”
)
group by a.student_id,a.score
having a.score <60;

— 32查询所有学生的选课情况;
select a.student_id,a.course_id,b.name,c.name
from student_course a,student b,course c
where a.student_id=b.id and a.course_id=c.id
group by a.student_id,a.course_id;

— 33 查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select a.student_id,a.course_id,a.score,b.name,c.name
from student_course a,student b,course c
where a.student_id=b.id and a.course_id=c.id
group by a.student_id,a.course_id,a.score
having a.score>70;

— 34 查询不及格的课程,并按课程号从大到小排列
select course_id,score
from student_course
group by course_id,score
having score<60
order by course_id;

— 35 查询课程编号为30003且课程成绩在80分以上的学生的学号和姓名;
select a.student_id,a.score,a.course_id,b.name
from student_course a ,student b
where a.student_id=b.id and a.course_id=”30003″
group by a.student_id,a.score,a.course_id
having a.score>80;

— 36求选了课程的学生人数
select count(distinct student_id) from student_course;

— 37 查询选修“liu”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select b.name,a.score
from student_course a,student b,course c,teacher d
where a.course_id=c.id and a.student_id=b.id and a.teacher_id=d.id and d.name=”liu”
and a.score=(select max(score) from student_course where course_id=c.id);

— 38查询各个课程及相应的选修人数
select course_id,count(student_id)
from student_course
group by course_id;

— 39 查询不同课程成绩相同的学生的学号、课程号、学生成绩
select a.student_id,a.course_id,a.score
from student_course a,student_course b
where a.score=b.score and a.course_id <> b.course_id;

— 40 查询每门功成绩最好的前两名
select * from student_course where score in(
select score from(
select score from student_course limit 2
) as a
)
order by course_id desc;

— 41 统计每门课程的学生选修人数(超过4人的课程才统计)。
— 要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,
— 若人数相同,按课程号升序排列
select course_id, count(*)
from student_course
group by course_id
having count(*)>4
order by count(*),course_id desc;

— 42 检索至少选修两门课程的学生学号
select student_id
from student_course
group by student_id
having count(*)>=2;

— 43 、查询全部学生都选修的课程的课程号和课程名
select *
from course
where id in(
select course_id from student_course group by course_id having count(*)=(select count(*) from student)
);

— 44 查询没学过“yang”老师讲授的课程的学生姓名
select name from student where id not in(
select distinct a.student_id
from student_course a,teacher c
where a.teacher_id=c.id and c.name = “yang”
);

— 45 查询两门以上不及格课程的同学的学号及其平均成绩
select student_id,avg(score)
from student_course
where score <60
group by student_id
having count(*) >=2;

— 46 检索“30004”课程分数小于60,按分数降序排列的同学学号
select student_id
from student_course
where course_id=”30004″ and score<60
group by student_id,score
order by score desc;

— 47 删除“10002”同学的“30001”课程的成绩
delete from student_course where student_id=10002 and course_id=30002;

 

创建表

//创建表
CREATE TABLE IF NOT EXISTS `student`(
'字段名' 列类型 [属性] [索引] [注释],
'字段名' 列类型 [属性] [索引] [注释],
......
'字段名' 列类型 [属性] [索引] [注释]
)[表的类型][字符集设置][注释]
//插入语句
INSERT INTO 表名([字段1,字段2..])VALUES('值1','值2'..),[('值1','值2'..)..];