一个复杂的delete语句 涉及4个表a(aid, bid) adi是主键,bid在这里是外键可重复
b(bid) bid是主键
c(cid,bid) cid是主键,bid在这里是外键可重复
d (did,cid)did是主键, d的父亲是c
现在是根据在a重复的bid,来删除d的所有重复的bid相关记录这里可以抛开b表, 直接在a c d里写sql。
我的写法
delete frome d where cid in (
select cid from c where bid in
(
select bid from a group by bid having count(*)>=2
))大家知道mysql的嵌套语句执行得不是很好,这里寻求有没有更好的写饭
b(bid) bid是主键
c(cid,bid) cid是主键,bid在这里是外键可重复
d (did,cid)did是主键, d的父亲是c
现在是根据在a重复的bid,来删除d的所有重复的bid相关记录这里可以抛开b表, 直接在a c d里写sql。
我的写法
delete frome d where cid in (
select cid from c where bid in
(
select bid from a group by bid having count(*)>=2
))大家知道mysql的嵌套语句执行得不是很好,这里寻求有没有更好的写饭
解决方案 »
- mysql5.1.55 ,屏蔽#log-bin=mysql-bin后,无法关闭二进制日志
- mysql如何对联合主键建立外键
- ado.net数据库访问问题,求解答!
- 数据库没有有效所有者
- order by 理解 请大虾们分别对以下没戏每项结果集进行分析
- mysql 关联查询语句问题
- 求教我机子上找不mysqld-nt.exe这个进程?打开mysql连接不上localhost?郁闷
- mysql中,想获得4-10行的信息:select * from table1 limit 3,10
- 列数据拆分问题
- mysql字符串GROUP_CONCAT拼接的时候,怎么指定分隔符为换行符呢?
- 这个查询语句怎么写?
- sql能否做到下面例子的link
inner join
(select bid from a group by bid having count(*)>=2) b
on c.bid=b.bid
顶一个,用not exists估计效率也高不过这个,所以不写了
我也写出
只是想深入明白 为什么在mysql里
inner join 要远远好过 select where id in (select ....)
看一下这个贴子中的讨论。
http://topic.csdn.net/u/20090626/16/65f043cf-b9d9-4707-b660-9857461177f4.html
delete 换成select × from 马上通过这里质疑
delete d inner join c on d.cid=c.cid
inner join
(select bid from a group by bid having count(*)>=2) b
on c.bid=b.bid 是否能编译通过??
INNER JOIN
(SELECT bid FROM a GROUP BY bid HAVING COUNT(*)>=2) b
ON c.bid=b.bid
可见mysql有很多自己的表达方式在delete里面是不能随便使用别名的。