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

ORDER BY 子句与DISTINCT 冲突
我想按KK_Topic里的addtime排序取出 
KK_Topic中BoardID不同的五条记录,写成这样 
select distinct top 5 KK_Topic.BoardID from KK_Topic,KK_Board where KK_Topic.BoardID=KK_Board.BoardID and KK_Board.ParentID=290 order by KK_Topic.addtime desc 
出错:ORDER BY 子句与 (KK_Topic.addtime) DISTINCT 冲突。 
  
请问该怎么写::?????????????????? 

比如说这是topic表 
Topicid title boardid addtime  
  1 abc 100 2007-1-1 
  2 era 101 2007-1-2 
  3 avx 102 2007-1-3 
  4 zcv 100 2007-1-4 
  5 jhv 100 2007-1-5 
  6 ztw 103 2007-1-6 
  7 xcv 102 2007-1-7 
  8 zww 104 2007-1-8 
  9 zqw 105 2007-1-9 
  10 zti 103 2007-1-10 
我想取出的是 这样一个记录集 
Topicid title boardid addtime  
  10 zti 103 2007-1-10 
  9 zqw 105 2007-1-9 
  8 zww 104 2007-1-8 
  7 xcv 102 2007-1-7 
  5 jhv 100 2007-1-5 
waiting ol....

------解决方案--------------------
SQL code

create table topic
(
Topicid int,
title nvarchar(10),
boardid int,
addtime datetime   
)
insert topic select 
   1,       'abc',       100,     '2007-1-1'  
union all select   2,       'era',       101,     '2007-1-2'  
union all select   3,       'avx',       102,     '2007-1-3'  
union all select   4,       'zcv',       100,     '2007-1-4'  
union all select   5,       'jhv',       100,     '2007-1-5'  
union all select   6,       'ztw',       103,     '2007-1-6'  
union all select   7,       'xcv',       102,     '2007-1-7'  
union all select   8,       'zww',       104,     '2007-1-8'  
union all select   9,       'zqw',       105,     '2007-1-9'  
union all select   10,      'zti',       103,     '2007-1-10'  

select top 5 boardid from 
(
    select boardid,max(addtime) as addtime
    from topic
    group by boardid
) tmp 
order by addtime desc