sql left outer join 排重问题!

「已注销」 2008-12-10 09:52:16


select table1.* from table1 left outer join table2 on table1.callnumber=table2.callnumber



此时,如果table2 中有重复记录 那么这条语句就会取出重复的记录,请问在使用 left outer join 时如何排重重复的记录? 用DISTINCT 不合适,TABLE1的字段太多了,还包含ID!
...全文
128 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2008-12-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 peacock 的回复:]
既然有重复值,为什么要SELECT *呢?比如有10条记录是重复的,那么到底这10条要取出哪一条?依据是什么?
[/Quote]

table2的数据是别人送给我的,但里面有重复值,如果有10条重复的就取时间最小的那条

如:

id data addtime
111 ssss 2008-12-09 10:11:13
222 ssss 2008-12-09 10:11:15

....

data 字段重复,就取addtime 最小那条 也就是 id 为 111的
peacock 2008-12-10
  • 打赏
  • 举报
回复
既然有重复值,为什么要SELECT *呢?比如有10条记录是重复的,那么到底这10条要取出哪一条?依据是什么?
coldfirezzz 2008-12-10
  • 打赏
  • 举报
回复
Table1记录太多,可以DISTINCT Table2啊。
select table1.* from table1 left outer join (select distinct callnumber from table2) table2 on table1.callnumber=table2.callnumber
quincystar 2008-12-10
  • 打赏
  • 举报
回复
select table1.* from table1 left outer join (select distinct callnumber from table2 order by addtime) table2 on table1.callnumber=table2.callnumber

52,781

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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