-1

我有一个按他们的美元金额列排序的团队列表,当 PHP 在我的页面上发布它时,它有一个根据每个团队的金额级别标记给每个团队的层/类别。例子:

SQL结果:

Team | Amount
  A    550.00
  B    1200.00
  C    320.00
  D    800.00

PHP 的布局如下:

B - Top tier (over $1,000)
D - Middle tier (over $500)
A - Middle tier (over $500)
C - Low tier (over $150)

现在我在页面上显示了更多的团队,我需要做的是他们仍然按页面上从最高到最低的数量顺序显示,我实际上需要 SQL 输出到他们的层,然后在每个层中按字母顺序排列层。例如(使用我上面的小型 PHP 输出示例),团队“D”和“A”都在同一层,但“A”应该按字母顺序显示在第一个。

4

3 回答 3

1
select teamname from (
    select teamname, 1 as ordern from teams where Amount>=1000
        union all
    select teamname, 2 from teams where Amount<1000 and Amount>=500
        union all
    select teamname, 3 from teams where Amount<500 and Amount>=150
        union all
    select teamname, 4 from teams where Amount<150
) as q
order by ordern DESC, teamname ASC
于 2013-10-11T19:37:51.193 回答
1

您需要先按数量订购,然后按团队订购。

select TeamName,'Top tier (over $1,000)'
from table where amount > 1000
union all
select TeamName,'Middle tier (over $500)'
from table where amount between 500 and 1000
union all
select TeamName,'Low tier (over $150)'
from table where amount between 150 and 500
order by 2,1
于 2013-10-11T19:38:27.077 回答
1

采用:

SELECT CASE WHEN Amount > 1000 THEN 4
            WHEN Amount > 500 THEN 3
            WHEN Amount > 150 THEN 2
            ELSE 1
       END Tier,
       Team, Amount
FROM Table
ORDER BY Tier DESC, Team
于 2013-10-11T19:38:52.690 回答