现有两张表 News表 和 NewsRead表 
用News表执行查询语句如下:
SELECT  PKID,WriteTime,ParentID,Topic FROM  News
  WHERE Type = 1and Status =2  order by PKID desc得到以下数据:
PKID       WriteTime           ParentID            Topic
192  2009-11-13 00:00:00.000     1048      测试信息_(委)局显示条数
176  2009-08-28 00:00:00.000     956       关于进一步做好纪检监察信访信息平台有关工作的通知
172  2009-08-26 00:00:00.000     943       已婚育龄妇女第二次妇检时间9月10日前
171  2009-08-26 00:00:00.000     942       请速交回公务卡申请表 
168  2009-08-26 00:00:00.000     935       24日至26日体检用NewsRead表执行查询语句如下:
select NewsId from NewsRead
得到以下数据:
NewsId
191
192
168
现在我要查询出News表里的PKID,WriteTime,ParentID,Topic这些数据按照News表的PKID不存在NewRead表NewsId优先升序然后在按照News表的PKID进行升序。
求助各位高手 仁兄 在线等候

解决方案 »

  1.   

    select * from
    (
     SELECT PKID,WriteTime,ParentID,Topic FROM News 
    WHERE Type = 1and Status =2  order by PKID desc 
    ) tt
    where PKID not in (select NewsId from NewsRead)
    order by PKID desc
      

  2.   

    分开查再做连接 用union all
      

  3.   

    SELECT  PKID,WriteTime,ParentID,Topic , (case when if not exists(select 1 from NewsId) then 0 else 1 end) tt FROM News 
    WHERE Type = 1and Status =2
    order by tt , PKID
      

  4.   

    SELECT  NEWS.PKID,WriteTime,ParentID,Topic FROM News 
    left join newsRead on news.pkid=newsread.newsid
    WHERE Type = 1 and Status =2  order by isnull(newsread.PKID,0) asc 
      

  5.   

    --try
    SELECT  PKID,WriteTime,ParentID,Topic FROM News t
    WHERE Type = 1and Status =2 
    order by case (select count(1) from NewsRead where NewsId=t.PKID)
    when 0 then 1 else 2 end,PKID
      

  6.   

    3楼我写错了.
    下面的语句可以了,我去掉了条件WHERE Type = 1and Status =2 ,你自己加上即可.
    create table tb1(PKID int,     WriteTime datetime,         ParentID  int,          Topic varchar(50))
    insert into tb1 values(192 , '2009-11-13 00:00:00.000' ,   1048  ,   '测试信息_(委)局显示条数') 
    insert into tb1 values(176 , '2009-08-28 00:00:00.000' ,   956   ,   '关于进一步做好纪检监察信访信息平台有关工作的通知') 
    insert into tb1 values(172 , '2009-08-26 00:00:00.000' ,   943   ,   '已婚育龄妇女第二次妇检时间9月10日前') 
    insert into tb1 values(171 , '2009-08-26 00:00:00.000' ,   942   ,   '请速交回公务卡申请表') 
    insert into tb1 values(168 , '2009-08-26 00:00:00.000' ,   935   ,   '24日至26日体检') 
    create table tb2(NewsId int)
    insert into tb2 values(191) 
    insert into tb2 values(192) 
    insert into tb2 values(168)
    goSELECT  PKID,WriteTime,ParentID,Topic , (case when not exists(select 1 from tb2 where tb2.NewsId = tb1.PKID) then 0 else 1 end) tt 
    FROM tb1
    order by tt , PKIDdrop table tb1 , tb2/*
    PKID        WriteTime                                              ParentID    Topic                                              tt          
    ----------- ------------------------------------------------------ ----------- -------------------------------------------------- ----------- 
    171         2009-08-26 00:00:00.000                                942         请速交回公务卡申请表                                         0
    172         2009-08-26 00:00:00.000                                943         已婚育龄妇女第二次妇检时间9月10日前                                0
    176         2009-08-28 00:00:00.000                                956         关于进一步做好纪检监察信访信息平台有关工作的通知                           0
    168         2009-08-26 00:00:00.000                                935         24日至26日体检                                          1
    192         2009-11-13 00:00:00.000                                1048        测试信息_(委)局显示条数                                      1(所影响的行数为 5 行)*/
      

  7.   

    你还可以直接在order by 中写.
    create table tb1(PKID int,     WriteTime datetime,         ParentID  int,          Topic varchar(50))
    insert into tb1 values(192 , '2009-11-13 00:00:00.000' ,   1048  ,   '测试信息_(委)局显示条数') 
    insert into tb1 values(176 , '2009-08-28 00:00:00.000' ,   956   ,   '关于进一步做好纪检监察信访信息平台有关工作的通知') 
    insert into tb1 values(172 , '2009-08-26 00:00:00.000' ,   943   ,   '已婚育龄妇女第二次妇检时间9月10日前') 
    insert into tb1 values(171 , '2009-08-26 00:00:00.000' ,   942   ,   '请速交回公务卡申请表') 
    insert into tb1 values(168 , '2009-08-26 00:00:00.000' ,   935   ,   '24日至26日体检') 
    create table tb2(NewsId int)
    insert into tb2 values(191) 
    insert into tb2 values(192) 
    insert into tb2 values(168)
    goSELECT  PKID,WriteTime,ParentID,Topic  
    FROM tb1
    order by (case when not exists(select 1 from tb2 where tb2.NewsId = tb1.PKID) then 0 else 1 end) , PKIDdrop table tb1 , tb2/*
    PKID        WriteTime                                              ParentID    Topic                                              tt          
    ----------- ------------------------------------------------------ ----------- -------------------------------------------------- ----------- 
    171         2009-08-26 00:00:00.000                                942         请速交回公务卡申请表                                         0
    172         2009-08-26 00:00:00.000                                943         已婚育龄妇女第二次妇检时间9月10日前                                0
    176         2009-08-28 00:00:00.000                                956         关于进一步做好纪检监察信访信息平台有关工作的通知                           0
    168         2009-08-26 00:00:00.000                                935         24日至26日体检                                          1
    192         2009-11-13 00:00:00.000                                1048        测试信息_(委)局显示条数                                      1(所影响的行数为 5 行)*/
      

  8.   

    select PKID,WriteTime,ParentID,Topic,
    case when PKID=(select NewsId from NewsRead) then 1 else 0 end as rank
     from
    (
     SELECT PKID,WriteTime,ParentID,Topic FROM News 
    WHERE Type = 1and Status =2
    ) New
    order by rank desc
      

  9.   

    create table tb1(PKID int,     WriteTime datetime,         ParentID  int,          Topic varchar(50))
    insert into tb1 values(192 , '2009-11-13 00:00:00.000' ,   1048  ,   '测试信息_(委)局显示条数') 
    insert into tb1 values(176 , '2009-08-28 00:00:00.000' ,   956   ,   '关于进一步做好纪检监察信访信息平台有关工作的通知') 
    insert into tb1 values(172 , '2009-08-26 00:00:00.000' ,   943   ,   '已婚育龄妇女第二次妇检时间9月10日前') 
    insert into tb1 values(171 , '2009-08-26 00:00:00.000' ,   942   ,   '请速交回公务卡申请表') 
    insert into tb1 values(168 , '2009-08-26 00:00:00.000' ,   935   ,   '24日至26日体检') 
    create table tb2(NewsId int)
    insert into tb2 values(191) 
    insert into tb2 values(192) 
    insert into tb2 values(168)select PKID,WriteTime,ParentID,Topic,
    case when PKID in (select NewsId from tb2) then 1 else 0 end as rank
     from tb1 
    order by rank asc
    PKID        WriteTime               ParentID    Topic                                              rank
    ----------- ----------------------- ----------- -------------------------------------------------- -----------
    176         2009-08-28 00:00:00.000 956         关于进一步做好纪检监察信访信息平台有关工作的通知                           0
    172         2009-08-26 00:00:00.000 943         已婚育龄妇女第二次妇检时间9月10日前                                0
    171         2009-08-26 00:00:00.000 942         请速交回公务卡申请表                                         0
    168         2009-08-26 00:00:00.000 935         24日至26日体检                                          1
    192         2009-11-13 00:00:00.000 1048        测试信息_(委)局显示条数                                      1(5 行受影响)