请问各位sql查询怎么把日期离当天比较近的数据排在前面距离当天比较远的排在后面?我在表中有个字段是记录数据写入时间的
sql代码:
select a.pArea,a.pStreet,b.pComname,b.pStreet,b.pSheBei,b.cLine,b.cDefinition,b.cVideotape,b.cRe,b.cTime
from tbl_Product a left join tbl_Condition b on(a.pComname=b.pComname) where 1=1请问要怎么改?SQLselect

解决方案 »

  1.   

    SELECT  a.pArea ,
            a.pStreet ,
            b.pComname ,
            b.pStreet ,
            b.pSheBei ,
            b.cLine ,
            b.cDefinition ,
            b.cVideotape ,
            b.cRe ,
            b.cTime
    FROM    tbl_Product a
            LEFT JOIN tbl_Condition b ON ( a.pComname = b.pComname )
    WHERE   1 = 1
    ORDER BY  b.cTime DESC  --时间排序
      

  2.   

    select top 10   --前10
    from TB
    order by time select top 10   --11到20
    from (select top 20 from tb order by time) T 
    order by time--依次类推
      

  3.   


    select * from
    (
    SELECT  a.pArea ,
    a.pStreet ,
    b.pComname ,
    b.pStreet ,
    b.pSheBei ,
    b.cLine ,
    b.cDefinition ,
    b.cVideotape ,
    b.cRe ,
    b.cTime,
    ROW_NUMBER()over (order by b.cTime desc) as id
    FROM    tbl_Product a
    LEFT JOIN tbl_Condition b ON ( a.pComname = b.pComname )
    WHERE   1 = 1
    ) temp 
    where id between 1 and 10 
    order by id 
      

  4.   


    SELECT  a.pArea ,        a.pStreet ,        b.pComname ,        b.pStreet ,        b.pSheBei ,        b.cLine ,        b.cDefinition ,        b.cVideotape ,        b.cRe ,        b.cTimeFROM    tbl_Product a        LEFT JOIN tbl_Condition b ON ( a.pComname = b.pComname )WHERE   1 = 1ORDER BY  b.cTime DESC  --时间排序在这个语句中怎么改?小弟初学不太懂
      

  5.   

    SELECT TOP 10
            *
    FROM    ( SELECT TOP 20
                        a.pArea ,
                        a.pStreet ,
                        b.pComname ,
                        b.pStreet ,
                        b.pSheBei ,
                        b.cLine ,
                        b.cDefinition ,
                        b.cVideotape ,
                        b.cRe ,
                        b.cTime
              FROM      tbl_Product a
                        LEFT JOIN tbl_Condition b ON ( a.pComname = b.pComname )
              WHERE     1 = 1
              ORDER BY  b.cTime DESC
            ) T
    ORDER BY ctime DESC
      

  6.   

    select * from 
    (
    select a.pArea,a.pStreet,b.pComname,b.pStreet as pStreet1,b.pSheBei,b.cLine,b.cDefinition,b.cVideotape,b.cRe,b.cTime as TIMES
    from tbl_Product a left join tbl_Condition b on(a.pComname=b.pComname) where 1=1
    )

    order by
    case when datediff(day,TIMES,getdate()) > 0 then dateadd(day, datediff(year, TIMES, getdate()), TIMES)
    when  datediff(day,TIMES,getdate()) < 0 then dateadd(day, datediff(year, TIMES, getdate()), TIMES)
    when  datediff(day,TIMES,getdate()) = 0 then dateadd(day, datediff(year, TIMES, getdate()), TIMES)
    end