找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 54|回复: 8

求一个SQL语法,谢谢 [已解决]

[复制链接]

8

主题

24

回帖

84

积分

注册会员

积分
84
发表于 2018-10-26 15:39:28 | 显示全部楼层 |阅读模式
本帖最后由 mrlink 于 2018-10-26 16:01 编辑

有表: wenzhang、yonghu

用户发表了文章,但是用户被删除了,但文章还存在。

现在想在 文章表 里删除 用户表 里没有存在的用户的文章。

请问需要怎么写呢?因为php程序老旧,在删除用户时没有连该用户的文章也一同删除,所以导致文章表里存在很多不存在用户发表的文章。


求一个在myadmin里执行的SQL语法,还有一个在php删除用户时加上的该用户发表的文章的语法。谢谢大佬。


回复

使用道具 举报

1

主题

48

回帖

119

积分

注册会员

积分
119
发表于 2018-10-26 15:57:25 | 显示全部楼层
delete from wenzhang where wenzhang.username not in ( select username from usertable)
回复

使用道具 举报

140

主题

881

回帖

2234

积分

金牌会员

积分
2234
发表于 2018-10-26 16:16:00 | 显示全部楼层
delete from wenzhang where userid no in (select userid from yonghu)
回复

使用道具 举报

130

主题

1163

回帖

2770

积分

金牌会员

积分
2770
发表于 2018-10-26 16:04:00 | 显示全部楼层
delete from wenzhang a where not exists(select 1 from yonghu where user_id = a.user_id)
回复

使用道具 举报

939

主题

6627

回帖

1万

积分

论坛元老

积分
16181
发表于 2018-10-26 15:53:37 | 显示全部楼层

zhughe 发表于 2018-10-26 15:46

delete from wenzhang a where not exists(select 1 from yonghu where user_id = a.user_id)

人家用户都删了,上哪来找user_id
回复

使用道具 举报

1

主题

84

回帖

189

积分

注册会员

积分
189
发表于 2018-10-26 15:51:18 | 显示全部楼层

the2ndface 发表于 2018-10-26 15:51

人家用户都删了,上哪来找user_id

不知道是不是我的表达有问题,总的一句话就是删除用户表里没有存在的用户的文章,因为文章表里有 userid,所以就是一个删除语法加判断该userid是否存在user表里的id
回复

使用道具 举报

446

主题

2824

回帖

7050

积分

论坛元老

积分
7050
发表于 2018-10-26 15:40:53 | 显示全部楼层

llwxxcn 发表于 2018-10-26 15:57

delete from wenzhang where wenzhang.username not in ( select username from usertable)



  谢谢大佬,OK了
回复

使用道具 举报

8

主题

24

回帖

84

积分

注册会员

积分
84
 楼主| 发表于 2018-10-26 16:02:00 | 显示全部楼层

the2ndface 发表于 2018-10-26 15:51

人家用户都删了,上哪来找user_id

not exists 没用过?


dream7758521 发表于 2018-10-26 15:51

delete from wenzhang where 1=1



大佬我用你的代码把裤子删了,被老板叼了一顿
回复

使用道具 举报

8

主题

24

回帖

84

积分

注册会员

积分
84
 楼主| 发表于 2018-10-26 15:46:31 | 显示全部楼层

zhughe 发表于 2018-10-26 16:10

not exists 没用过?



真没用过这么高端的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Discuz! X

GMT+8, 2025-3-6 21:57 , Processed in 0.023778 second(s), 4 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表