SQL查询语句去重查询方法

一张表Table内
有字段A,B,C,
A有重复的数据,B没有重复的,C为时间段
想查询所有数据中,根据C字段时间保留的最新的数据

img


select t2.* from (select a , max(c) mc from tablename group by a) t left join tablename t2 on t.a= t2.a and t.mc = t2.c

试试

你看这样可行

SELECT DISTINCT a.*,b.number,c.time FROM
INNER JOIN b ON a.id=b.id
a WHERE a.time=(SELECT TOP 1 c.time FROM c ORDER BY c.time DESC)

SELECT a.* FROM (
select * FROM table ORDER BY C DESC
) a GROUP BY a.A

先写个子查询根据C倒叙,然后主查询根据A分组
如果能帮到你,望【采纳】


select distinct
a.name,a.id,a.time*,from 
table  a
union
select * from table b
union 
 select max(time) * from table c 

SELECT * FROM aaa GROUP BY name HAVING COUNT(name) >1;这样试试?name是你的字段

img

img