如何根据排名编写选择查询?

时间:2012-11-21 13:09:00

标签: sql

我有一张桌子

表1

id   city    projects       Rank
----------------------------------
1    che        p2           2
2    bang       p1           1
3    che        p4           1
4    bang       p3           2
5    bang       p5           3
6    gur        p6           1
7    gur        p7           2

基于排名我想选择城市和项目 我想要这样

   bang       p1            1
    che        p4           1
    gur        p6           1
    che        p2           2
    bang       p3           2
    gur        p7           2
    bang       p5           3

从table1中选择*,其中Rank =?

6 个答案:

答案 0 :(得分:2)

select city,project,rank from table1 order by rank,project,city

答案 1 :(得分:1)

我希望这会对你有所帮助:

select city, projects, Rank from table1 
order by Rank, city, projects

您也可以使用order by子句中的位置:

select city, projects, Rank from table1 
order by 3,1,2

底部有0的解决方案:

 select city, projects, Rank from table1 
    order by
    case 
     when Rank>0 then 1
     else 2
    end,
    Rank, city, projects

答案 2 :(得分:1)

试试这个

 select city, projects, Rank from table order by Rank

答案 3 :(得分:1)

在查询中使用ORDER BY子句。

SELECT city, projects, rank FROM table1 
order by rank

答案 4 :(得分:1)

这里很多答案都很好。

然而,通常的做法是在SQL中使用UPPERCASE语言结构:

SELECT city,project,rank FROM table1 ORDER BY rank,project;

: - )

答案 5 :(得分:0)

试试这个......

select city,projects,rank from table1 group by rank order by rank,city,projects;