我有一个按其美元金额列排序的团队列表,并且当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"应首先按字母顺序显示。
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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