我希望在网页上显示足球联赛表的片段。 我希望它选择一个特定的团队(团队 b),然后选择上面一位和下面一位的团队。
例如。
p w d l
team c 1 1 0 0
team b 1 0 1 0
team a 1 0 0 1
下周可能是:
p w d l
team d 2 2 0 0
team b 2 1 1 0
team c 2 1 0 1
等等。
如何最好地解决这个问题?是在 MySQL 中使用查询还是我希望在 PHP 中执行此操作?
谢谢
最佳答案
select * from games order by w;
+--------+------+------+------+------+
| name | p | w | d | l |
+--------+------+------+------+------+
| team b | 1 | 0 | 1 | 0 |
| team a | 1 | 0 | 0 | 1 |
| team c | 1 | 1 | 0 | 0 |
| team d | 2 | 2 | 0 | 0 |
+--------+------+------+------+------+
4 rows in set (0.00 sec)
mysql> SELECT NAME,P,W,D,L,@curRank := @curRank + 1 as rank FROM
(按 w DESC 从游戏顺序中选择 NAME、P、W、D、L)x,(SELECT @curRank := 0) r;
+--------+------+------+------+------+------+
| NAME | P | W | D | L | rank |
+--------+------+------+------+------+------+
| team d | 2 | 2 | 0 | 0 | 1 |
| team c | 1 | 1 | 0 | 0 | 2 |
| team b | 1 | 0 | 1 | 0 | 3 |
| team a | 1 | 0 | 0 | 1 | 4 |
+--------+------+------+------+------+------+
4 rows in set (0.01 sec)
mysql> SELECT NAME,P,W,D,L,rank FROM
(SELECT NAME,P,W,D,L,@curRank := @curRank + 1 as rank,
if(name ="team b",@curRank := @curRank*-1,0) AS required_rank
FROM (select NAME,P,W,D,L from games order by w DESC) x,
(SELECT @curRank := 0) r ) p order by abs(rank) desc limit 3;
+--------+------+------+------+------+------+
| NAME | P | W | D | L | rank |
+--------+------+------+------+------+------+
| team b | 1 | 0 | 1 | 0 | 3 |
| team c | 1 | 1 | 0 | 0 | 2 |
| team a | 1 | 0 | 0 | 1 | -2 |
+--------+------+------+------+------+------+
3 rows in set (0.00 sec)
关于php - 选择行上方和下方的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38128548/