求一个处理重复行的高效语句,有1万条数据

aitxt 2013-09-29 02:12:22
删除title字段相同的行
如果content字段有值则优先保留,
如果title相同的行,且content都没有内容或者都有内容,保留ID小的

举例如下:

原表
id title content
1 狮子 在路上
2 狮子
3 老虎
4 老虎 嘻嘻嘻
5 豹子 哈哈哈
6 豹子
7 狮子 在路上
8 骆驼
9 骆驼

结果
id title content
1 狮子 在路上
4 豹子 哈哈哈
5 老虎 嘻嘻嘻
8 骆驼
...全文
112 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kobe8free 2013-09-29
  • 打赏
  • 举报
回复
引用 1 楼 rucypli 的回复:
select T1.id,T1.title,T2.content from ( select * from tb A where not exists (select 1 from tb where A.title=title and A.id>id) ) T1 left join ( selet * from tb where content is not null )T2 on T1.title=T2.title
如果left join 的 是selet * from tb where content is not null 那么结果集是不是连骆驼这条数据也显示不出来了?
rucypli 2013-09-29
  • 打赏
  • 举报
回复
select T1.id,T1.title,T2.content from ( select * from tb A where not exists (select 1 from tb where A.title=title and A.id>id) ) T1 left join ( selet * from tb where content is not null )T2 on T1.title=T2.title

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧