我有这张桌子
| Name | Born | Live | Age |
|-------- |------- |-------- |----- |
| John | UK | UK | 10 |
| Smith | UK | France | 12 |
| Ben | Spain | Spain | 15 |
| George | Spain | UK | 25 |
| Ann | UK | UK | 27 |
| Cassie | Spain | Spain | 30 |
我想要这个
| Name | Born | Live | Age | | Rank |
|-------- |------- |-------- |----- | |------|
| John | UK | UK | 10 | | 1 |
| Ann | UK | UK | 27 | | 2 |
| Smith | UK | France | 12 | | 1 |
| Ben | Spain | Spain | 15 | | 1 |
| Cassie | Spain | Spain | 30 | | 2 |
| George | Spain | UK | 25 | | 1 |
所以我希望通过出生和生活获得排名。
我知道在SQL中我们可以使用RANK AND PARTITION,但在MYSQl中我找不到这样做的方法。有人可以帮忙吗?
答案 0 :(得分:1)
我想你应该看一下:Rank function in MySQL
希望它会对你有帮助;)!
答案 1 :(得分:1)
SELECT Name, Born, Live,Age,
@rank := IF(@prev_born = Born, @rank + 1,1) AS Born_rank,
@prev_Born := Born
FROM TABLE,(select @prev_born :=0,@rank :=0) r
ORDER BY Born, Age ASC