Sql语句问题!
select top 6 *
from (select BlogID,Hits=max(Hits) from Blog_Article group by BlogID) b
order by b.Hits desc
以上Sql语句查询正确,但是我想
select BlogID,Hits=max(Hits) from Blog_Article group by BlogID
这里查询的时候多返回一个Title字段,但是又不能让他参与group by,该怎么办?
------解决方案--------------------那在title前也加一个max试试
------解决方案----------------------如果你的BlogID只對應一個Title,那麼可以這樣
select BlogID,Hits=max(Hits), Title from Blog_Article group by BlogID, Title
--如果你的BlogID對應多個Title,查詢的時候,你希望取哪個Title,如果是隨便取一個,可以這樣
select BlogID,Hits=max(Hits), Title = max(Title) from Blog_Article group by BlogID
--或者
select BlogID,Hits=max(Hits), Title = min(Title) from Blog_Article group by BlogID
------解决方案--------------------另外,你的語句可以做下簡化
select top 6 *
from (select BlogID,Hits=max(Hits) from Blog_Article group by BlogID) b
order by b.Hits desc
改為
select top 6 BlogID,Hits=max(Hits) from Blog_Article group by BlogID
order by Hits desc