首页 > 试题广场 >

Mysql中表student_table(id,name,b

[单选题]
Mysql中表student_table(id,name,birth,sex),删除name重复的id最大的记录,比如'张三'重复2次,id分别是1、2,则删除id=2的记录,保留id=1的记录。如下SQL正确的是()?
  • delete from student_table where id in (
    select t2.*
    from
    (select name,count(*) as c1 from student_table GROUP BY name having c1 > 1)t1
    left join
    (select name, max(id) as id from student_table group by name ) t2
    on t1.name = t2.name ) ;
  • delete from student_table t0 
    inner join (
    select t2.*
    from
    (select name,count(*) as c1 from student_table GROUP BY name having c1 > 1)t1
    left join
    (select name, max(id) as id from student_table group by name ) t2
    on t1.name = t2.name ) t3
    on t0.id = t3.id ;
  • delete t0
    from student_table t0
    inner join (
    select t2.*
    from
    (select name,count(*) as c1 from student_table GROUP BY name having c1 > 1)t1
    left join
    (select name, max(id) as id from student_table group by name ) t2
    on t1.name = t2.name ) t3
    on t0.id = t3.id ;
  • delete student_table
    from student_table t0
    inner join (
    select t2.*
    from
    (select name,count(*) as c1 from student_table GROUP BY name having c1 > 1)t1
    left join
    (select name, max(id) as id from student_table group by name ) t2
    on t1.name = t2.name ) t3
    on t0.id = t3.id ;
应该是D吧。
A.in后面的子查询结果是2个字段,id是一个字段。语法错误。
B C D,多表删除的时候,删除哪个表的信息就在delete 后面加上表名,eg: delete 表1,表2 from 表1 inner join 表2
发表于 2022-01-05 16:35:16 回复(0)
题目要求:1.删除name重复的id  2.最大的记录
id
name
birth
sex
1 a
2 b

3 a

4 a

5 b

6 c

按照要求我们需要删除编号4和编号5.


先求出有重复的id的表t1
select name,count(*) as c1 
from student_table 
GROUP BY name  having c1 > 1
name c1(次数)
a 3
b 2


求出每个人id最大值的表t2
select name, max(id) as id 
from student_table 
group by name 
name id(最大的id值,可能不是重复的)
a 4
b 5
c 6


t2这个表里面有id不重复的,所以与t1表left join 
将两表连接得到  每个人id不止一个并且id的最大值的表 t3
这个时候t3表里面字段有 name  
select t2.* from t1 left join t2 on t1.name = t2.name
name c1 id
a 3 4
b 2 5

delete 和inner join 一起使用的时候需要标明表名
delete t0
from student_table t0 inner join t3 
on t0.id = t3.id ;



编辑于 2022-02-21 17:53:33 回复(10)
A执行报错【1241 - Operand should contain 1 column(s)】,in的子句中只能含有1个字段,t2.*含有t2.name和t2.id这2个字段,写成t2.id才对。
B执行报错【1064 - You have an error in your SQL syntax;】,多表删除时,delete和from之间必须要写明想要删除记录的表名。
C和D的区别就是表名,一个写的原始表名,一个写的别名。当起了别名时,本条语句(不包含子语句)必须使用别名,而不能使用原名,否则报错。
发表于 2022-06-14 10:58:08 回复(2)
1 、结果使用内联接,不能使用==否则会删除所有的id 2、当update和delete和 inner join一起使用时候,需要标明表名称
发表于 2021-12-13 23:30:10 回复(0)
使用这个,更简单,可读性更强,也不用分表
delete from student_table where id in 
(select maxid from 
(select  max(id) maxid  from  student_table group by name having count(name)>1) a);
发表于 2022-05-15 15:15:25 回复(1)
这sql为啥写的这么啰嗦
发表于 2022-01-13 18:01:20 回复(0)
。。。
发表于 2021-12-10 15:29:52 回复(0)
好啰嗦的sql语句,
DELETE FROM student_table WHERE id IN ( SELECT id FROM ( SELECT MAX(id) AS id FROM student_table GROUP BY name HAVING COUNT(*) > 1 ) AS t
);
发表于 2023-09-02 18:02:15 回复(1)
感觉不对
发表于 2021-12-11 15:13:11 回复(2)
delete from table where ****
发表于 2022-09-23 09:26:12 回复(0)
关于我看错题目,结果选对了这件事
发表于 2022-07-22 17:01:14 回复(0)
这b 那里错了
发表于 2022-04-14 10:13:35 回复(0)
为什么只要t0.id=t3.id 就行了,不需要t0.name=t3.name 嘛
发表于 2022-03-24 15:16:53 回复(0)
看着C和D,就delete t0 和 delete student_table 的区别,一个是原表名一个是别名,这个有什么说明吗,求解

发表于 2022-02-11 18:10:33 回复(3)