inner jion 后的重复问题

bdzm 2010-05-13 10:37:57
现在有两个表t1,t2

t1表中有c1,c2,c3....字段,t2种有c3,c4....和其他字段

where条件都在t1表里,
以前语法:

select t1.*,t2.*
from t1 inner jion
t2 on t1.c3=t2.c3
where t1.c1=1 and t1.c2=1

以前t2中的c3都是唯一的,现在可能不唯一,如果t2中的c3出现两条记录的话,结果也回出现两条重复记录。

过滤掉结果中的重复应该怎么写?
请高人指导!
...全文
71 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdzm 2010-05-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sql_lover 的回复:]
--去掉主键值大的记录

SQL code

with t as
(select * from t2 a where not exists(select 1 from t2 where 主键>a.主键 and c3=a.c3))
select t1.*,t.*
from t1 inner jion
t on t1.c3=t.c3 and t1.c1=1 and t1……
[/Quote]


虽然写法不一样,谢谢提示,结帖
rmljoe 2010-05-13
  • 打赏
  • 举报
回复
select distinct t1.*,t2.*
from t1 inner jion
t2 on t1.c3=t2.c3
where t1.c1=1 and t1.c2=1

加distinct试试
sql_lover 2010-05-13
  • 打赏
  • 举报
回复
--去掉主键值大的记录

with t as
(select * from t2 a where not exists(select 1 from t2 where 主键>a.主键 and c3=a.c3))
select t1.*,t.*
from t1 inner jion
t on t1.c3=t.c3 and t1.c1=1 and t1.c2=1
永生天地 2010-05-13
  • 打赏
  • 举报
回复
很难,除非先在子查询里进行过滤
youyou2404 2010-05-13
  • 打赏
  • 举报
回复
嘛意思 inner join后会出现多行

你列出数据及你想要的结果》
kingthinker 2010-05-13
  • 打赏
  • 举报
回复
on t1.c3=t2.c3
and 其他条件

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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