mnbn.net
当前位置:首页 >> sql A,B两表连接,B表中重复记录只取一条 >>

sql A,B两表连接,B表中重复记录只取一条

那就用显示B表排除他的重复项,再去用A表去匹配他,左连接得出匹配的项 Select * From (Select Distinct B.* From B) as 查询1 Left Join A on 查询1.id=A.id where(B.id IS NOT NULL); 要先把B表重复项整理掉,再去匹配,还要把NULL项去掉

select max(id) id,name from 表 group by name --如果表中还有其他字段要查出,那么 select * from 表 where id in (select max(id) from 表 group by name )

select * from (select T.*,ROW_NUMBER() OVER (ORDER BY b中重复字段 ) rn from (A LEFT JOIN b) T) where rn = 1 大概思路 自己研究哈 呵呵

SELECT MIN(B.flow_id),A.item_ID FROM Test_Table A INNER JOIN Test_Table B ON A.Flow_ID!=B.Flow_ID AND A.Item_ID=B.Item_ID WHERE A.def_sup_flag=1 GROUP BY A.item_ID--应该是这样写吧--不过我觉得你是要实现的功能是不是,def_sup_flag...

代码如下: select * from tbl_DPImg where ID in (select min(ID) from tbl_DPImg group by DPID)处理后结果为: 查找表中多余的重复记录,重复记录是根据单个字段(teamId)来判断 select * from team where teamId in (select teamId from tea...

sql语句中,关联查询只取分组的一条记录的方法如下: select userid,ranking,username from table //查询字段有userid username where userid+ranking in //where 条件包括userid+ranking in是集合选择关键字 ( select userid+m...

select id, name ,date, min(value) from T where value '' group by id,name ,date

select max(id) id,name from 表 group by name --如果表中还有其他字段要查出,那么 select * from 表 where id in (select max(id) from 表 group by name )

IF OBJECT_ID('Table_2') > 0 DROP TABLE Table_2 GO CREATE TABLE [dbo].[Table_2] ( [tel] [INT] NOT NULL , [time] [DATETIME] NOT NULL , [name] [NVARCHAR](50) NULL ) ON [PRIMARY] INSERT dbo.Table_2 ( tel, time, name ) VALUES ( 100...

select max(id),姓名,min(职业) from 表名 group by 姓名

网站首页 | 网站地图
All rights reserved Powered by www.mnbn.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com