『壹』 sql一條語句來表示前三項升序排序,其他項降序
實現:
1
2
3
5
4
的效果:
假設你要的欄位為:col_you_want , 類型為 int
select col_you_want
from mytable a
order by
case when identity(int,1,1) <= 3 then
identity(int,1,1) end asc ,
col_you_want desc
;
『貳』 SQL如何對分組後的結果進行排序並且取前幾名
SQL取分組中的前幾名
[sql] www.2cto.com
create table #aa(class varchar(10),name varchar(10),lang int,math int)
go
insert into #aa
select '1','a1',60,70
union all
select '1','a2',80,70
union all
select '1','a3',90,70
union all
select '1','a4',50,70
go
insert into #aa
select '2','b1',60,70
union all
select '2','b2',90,90
union all
select '2','b3',90,70
union all
select '2','b4',50,70
go
select * from #aa
--取每年級前一名的成級
select * from
(select ROW_NUMBER() over(partition by class order by lang+math desc) rr, * from #aa ) a
where rr<2
--取每年級前二名的成級
select * from
(select ROW_NUMBER() over(partition by class order by lang+math desc) rr, * from #aa ) a
where rr<3
『叄』 使用SQL列出表中按Score排序的前三名的結果
select
top
3
with
ties
name,score
from
record
order
by
score
(由低分到高分排列)
select
top
3
with
ties
name,score
from
record
order
by
score
desc(由高分到低分排列)
with
ties
的用法:對於TOP來說with
ties
很重要,
他能在顯示的行後附加多個和最後一行取值相同的行.
『肆』 SQL語句 怎樣按排序後顯示前三個
沒有看到你的資料庫conn連接啊:
@$conn = mysql_connect("yourHost","yourUserID","yourPassword");
$sql=mysql_query("select top 3 title from art_title join art_neirong on art_title.no=art_neirong.no order by last_update DESC;",$conn);
//echo $sql;
$i=0;
while($result=mysql_fetch_array($sql))
{
echo "<h5>".$result['title']."</h5>;
}
mysql_close($conn);
『伍』 SQL怎麼取出每個科目前三名並按科目和分組排列
select B1.姓名,B1.科目,B1.分數 from B B1 where(select count(1) from B where 科目=B1.科目 and 分數〉=B1.分數)〈=3 order by B1.科目,B1.分數;
『陸』 用SQL選出每個人成績的最高的前三條紀錄
--用開窗函數每個用戶成績排序
select*from
(selectt.*,row_number(partitionby用戶名orderby成績desc)asflagfrom表名t)
whereflag<=3
『柒』 如何在SQL里讓指定的列不管怎樣排序總是排名前三
select * from stuInfo where stuName='張三'
union
select * from stuInfo where stuName='李四'
union
select * from stuInfo where stuName='王五'
union
(select * from stuInfo where stuName not in('張三','李四','王五'))
這樣 你後面的怎麼排 前三行都是你指定的
『捌』 一條sql語句 按照時間取出置頂的前三條,在按照時間排序,返回一個結果集,就想資訊的列表頁,改怎麼寫哪
樓主 你理解錯了,不信你去問你們導師。
所謂"取出置頂的前三條 再按時間排序"
這個並不是說把前三條排除之後進行時間排序。
面是說就要前面的那三條……
1樓的回答是正確的
『玖』 sql 升序降序排列
降序:SELECT * FROM kc ORDERBYcpbh DESC
升序:SELECT * FROM kc ORDERBYcpbhASC
語法:
sql可以根據欄位進行排序,其中,DESC表示降序,ASC表示升序
order by 欄位名 DESC;按照欄位名降序排序
order by 欄位名 ASC;按照欄位名升序排序
實例:
一、/*查詢學生表中姓名、學號,並以學號降序排序*/
select name,StuID from Students_information order by StuID desc /**order by 以什麼排序,默認為升序,desc是降序*/
二、/*查詢學生表中前5名學生的姓名,學號,並以學號升序排列*/
select top 5 name,StuID from Students_information order by StuID /*order by 默認為升序*/
(9)sql前三項排序擴展閱讀:
一、ORDER BY 語句
ORDER BY 語句用於根據指定的列對結果集進行排序。
ORDER BY 語句默認按照升序對記錄進行排序。
如果您希望按照降序對記錄進行排序,可以使用 DESC 關鍵字。
二、SQL 排序多個欄位
order by 多個欄位,每個欄位後面都有排序方式,默認ASC
例如:select table a order by a.time1 ,a.time2 desc,a.time3 asc
『拾』 sql 查詢每個分組下的前幾條記錄並排序
比如 表 table1,分類 type,讀取分類前5條,合並在聯查,並且排序
select * from
(
SELECT top 5 * FROM table1 where type=1
UNION
SELECT top 5 * FROM table1 where type=2
UNION
SELECT top 5 * FROM table1 where type=3
) as table_new order by id desc