要在表中查出前8条符合条件的,含有IID,Title,WebPath,Uploadtime这些字段的数据,现在用的语句是:
SELECT TOP 8 IID, Title, WebPath, UploadTime
FROM SinaCF_NormalInfo
WHERE (PageMain = 0) AND (Title LIKE '%民工%') OR
      (PageMain = 0) AND (KeyWords LIKE '%民工%')
ORDER BY UploadTime DESC
就是要查出在title或者keywords字段中含有查询条件的前8条记录
现在出现的问题就是查出来的记录中有title相同的,现在要把title相同的去掉,我想应该是要用到distinct,但又不会用,请大家指教

解决方案 »

  1.   

    SELECT TOP 8 IID, Title, WebPath, UploadTime
    FROM SinaCF_NormalInfo a
    where not exists (select 1 from SinaCF_NormalInfo where IID >A.IID and title =a.title )
    and ((PageMain = 0) AND (Title LIKE '%民工%') )OR
    ( (PageMain = 0) AND (KeyWords LIKE '%民工%'))
    ORDER BY UploadTime DESC
    这个试试...
      

  2.   

    主要是这一句对吧:not exists (select 1 from SinaCF_NormalInfo where IID >A.IID and title =a.title )--
    不存在 SinaCF_NormalInfo 表中 当title相等的时候 a的iid是最大的  
    也就是相同 title情况下 取最大iid的值...
      

  3.   

    SELECT TOP 8 IID, Title, WebPath, UploadTime
    FROM SinaCF_NormalInfo
    WHERE ((PageMain = 0) AND (Title LIKE '%民工%') OR
          (PageMain = 0) AND (KeyWords LIKE '%民工%')) and IID not exists (select min(IID) from SinaCF_NormalInfo group by title)
    ORDER BY UploadTime DESC
      

  4.   

    楼主试过 jiangsh100(著名菜鸟) 的吗?好像不对