日期:2014-05-18  浏览次数:20434 次

跪求一个SQL语句
有表info   (id   ,   showname   ,   time   ,   top_flag   ,   gsid)
要求从表中取出全部字段,gsid字段值相同的记录只取一条,并且选择出来的记录按照time降续排列.
我写了下面几个都没用啊,各位高手帮帮忙啊  

SELECT   top   30   id,showname,time,top_flag,gsid  
FROM   info   A  
where   not   exists   (select   1   from   info   where   gsid=A.gsid   and   time> A.time)   and   flag=1   order   by   top_flag   desc,dateandtime   desc
这个语句还是选出了重复gsid的记录.

SELECT     top   30   id,showname,time,top_flag,gsid  
from   info   where   time   in   (select   max(time)   from   info   group   by   gsid)   and   flag=1   order   by   top_flag   desc,time   desc
这条语句竟然也选出了重复gsid的记录!!!!
各位大虾帮帮忙   这个句子到底该怎么写啊   ?

------解决方案--------------------
上面错了.
select t.* from
(
select a.* from info a,
(select gsid , max(time) as time from info group by gsid) b
where a.gsid = b.gsid and a.time = b.time
) t
order by time desc
------解决方案--------------------
SELECT top 30 id,showname,time,top_flag,gsid
FROM info A
where not exists (select 1 from info where gsid=A.gsid and (time> A.time or time=A.time and id> a.id))
and flag=1 order by top_flag desc,dateandtime desc