我需要建议。
我有一个简单的while循环。 我有一个桌上的参赛者。每位参赛者都有一栏[celkem]。
这些数据摘录了
时的循环当你有两个点时我也需要它,所以它总是在范围内(见图)
其余的摘录如下:$ row ['name']; $ row ['bodycelkem']
我的问题是,如何使用PHP和MySQL转储这个范围的顺序?
编辑: SQLFiddler
(我需要在范围内指定自动排名)
答案 0 :(得分:1)
由于Poradi
信息不存在,您需要对其进行推断。你可以用变量做到这一点。请参阅下面的SQL或SQLFiddle
SELECT `range`, jmeno, rangeData.celkem FROM `hraci`
LEFT JOIN (
SELECT
a1.*,
if( range_start = range_end,
range_start, CONCAT(range_start,", - ",range_end)
) `range`
FROM (
SELECT
q1.*,
(@runtot + 1) AS range_start,
(@runtot := @runtot + q1.num) range_end
FROM (
SELECT @rn:=@rn+1 rank, t1.num, t1.celkem FROM (
SELECT celkem, count(celkem) num FROM hraci GROUP BY celkem ORDER BY celkem DESC
) t1, (SELECT @rn:=0) t2
) q1, (SELECT @runtot:=0) q2
) a1
) rangeData ON hraci.`celkem` = rangeData.`celkem`
这是如何运作的?
答案 1 :(得分:1)
这是一个想法
DROP TABLE IF EXISTS hraci;
CREATE TABLE hraci (
jmeno VARCHAR(45) NULL,
celkem DOUBLE NULL)
ENGINE = InnoDB;
INSERT INTO hraci
(jmeno, celkem) VALUES
('Dan', 97.5),
('Adam', 97.2),
('Petr', 90.5),
('Pavel', 90.5),
('Michal', 87.3),
('Jan', 87.3),
('David', 87.3),
('Tomás', 87.3),
('Jarda', 85.2);
SELECT a.celkem
, a.jmeno
, CASE WHEN MIN(b.rank)-1 <> a.rank THEN CONCAT(a.rank,' - ',MIN(b.rank) - 1) ELSE a.rank END rank
FROM
( SELECT celkem
, jmeno
, FIND_IN_SET(celkem,celkems) rank
FROM hraci
CROSS
JOIN
( SELECT GROUP_CONCAT( celkem ORDER BY celkem DESC) celkems
FROM hraci
) x
) a
LEFT JOIN
( SELECT celkem
, jmeno
, FIND_IN_SET(celkem,celkems) rank
FROM hraci
CROSS
JOIN
( SELECT GROUP_CONCAT( celkem ORDER BY celkem DESC) celkems
FROM hraci
) y
) b
ON b.rank > a.rank
GROUP BY a.celkem,a.jmeno;
+--------+--------+-------+
| celkem | jmeno | rank |
+--------+--------+-------+
| 85.2 | Jarda | 9 |
| 87.3 | David | 5 - 8 |
| 87.3 | Jan | 5 - 8 |
| 87.3 | Michal | 5 - 8 |
| 87.3 | Tomás | 5 - 8 |
| 90.5 | Pavel | 3 - 4 |
| 90.5 | Petr | 3 - 4 |
| 97.2 | Adam | 2 |
| 97.5 | Dan | 1 |
+--------+--------+-------+