我正在 XAMMP 中使用 PHP ang MySQL 或 PhpMyAdmin 创建一个选美评分系统。我以某种方式设法使用 MySQL 获得候选人的排名,但有些情况下可以取得联系。如何打破使用 PHP 查询结果的平局?
候选人的排名是根据他们在一个有5名评委和9名候选人的类别中的分数进行的。所以我真的不知道该怎么做或如何在 PHP 中解决它。
如果我希望 C9 排在第一而不是 C4 怎么办?
这是我的数据库...希望有人可以帮助我。提前致谢。
CREATE TABLE IF NOT EXISTS score (
candidate_no varchar(5) NOT NULL ,
category_no varchar(5) NOT NULL ,
judge_id varchar(5) NOT NULL ,
score int(3),
PRIMARY KEY (candidate_no,category_no,judge_id),
KEY score_fkey (judge_id),
KEY score_fkey3 (category_no)) ;
INSERT INTO score (candidate_no, category_no, judge_id, score) VALUES
('C1', 'cat1', 'J1', 17),
('C1', 'cat1', 'J2', 15),
('C1', 'cat1', 'J3', 17),
('C1', 'cat1', 'J4', 18),
('C1', 'cat1', 'J5', 19),
('C2', 'cat1', 'J1', 17 ),
('C2', 'cat1', 'J2', 15 ),
('C2', 'cat1', 'J3', 16 ),
('C2', 'cat1', 'J4', 18 ),
('C2', 'cat1', 'J5', 18 ),
('C3', 'cat1', 'J1', 15 ),
('C3', 'cat1', 'J2', 20 ),
('C3', 'cat1', 'J3', 19 ),
('C3', 'cat1', 'J4', 16 ),
('C3', 'cat1', 'J5', 19 ),
('C4', 'cat1', 'J1', 19 ),
('C4', 'cat1', 'J2', 20 ),
('C4', 'cat1', 'J3', 18 ),
('C4', 'cat1', 'J4', 18 ),
('C4', 'cat1', 'J5', 19 ),
('C5', 'cat1', 'J1', 18 ),
('C5', 'cat1', 'J2', 16 ),
('C5', 'cat1', 'J3', 18 ),
('C5', 'cat1', 'J4', 18 ),
('C5', 'cat1', 'J5', 18 ),
('C6', 'cat1', 'J1', 20 ),
('C6', 'cat1', 'J2', 16 ),
('C6', 'cat1', 'J3', 16 ),
('C6', 'cat1', 'J4', 16 ),
('C6', 'cat1', 'J5', 17 ),
('C7', 'cat1', 'J1', 11 ),
('C7', 'cat1', 'J2', 12 ),
('C7', 'cat1', 'J3', 14 ),
('C7', 'cat1', 'J4', 15 ),
('C7', 'cat1', 'J5', 17 ),
('C8', 'cat1', 'J1', 15 ),
('C8', 'cat1', 'J2', 16 ),
('C8', 'cat1', 'J3', 18 ),
('C8', 'cat1', 'J4', 17 ),
('C8', 'cat1', 'J5', 17 ),
('C9', 'cat1', 'J1', 19 ),
('C9', 'cat1', 'J2', 19 ),
('C9', 'cat1', 'J3', 19 ),
('C9', 'cat1', 'J4', 19 ),
('C9', 'cat1', 'J5', 18 );
这是我的查询:
select sum
,candidate_no
,@curRank := @curRank + 1 AS rank
from(
select sum(score) / 5 sum
,candidate_no
from score
where candidate_no = 'c1'
union
select sum(score) / 5 sum
,candidate_no
from score
where candidate_no = 'c2'
union
select sum(score) / 5 sum
,candidate_no
from score
where candidate_no = 'c3'
union
select sum(score) / 5 sum
,candidate_no
from score
where candidate_no = 'c4'
union
select sum(score) / 5 sum
,candidate_no
from score
where candidate_no = 'c5'
union
select sum(score) / 5 sum
,candidate_no
from score
where candidate_no = 'c6'
union
select sum(score) / 5 sum
,candidate_no
from score
where candidate_no = 'c9'
) a , (SELECT @curRank := 0) r
order by sum desc
结果将是..
sum | candidate_no | rank
18.8000 | C4 | 1
18.8000 | C9 | 2
17.8000 | C3 | 3
17.6000 | C5 | 4
17.2000 | C1 | 5
17.0000 | C6 | 6
16.8000 | C2 | 7
16.6000 | C8 | 8
13.8000 | C7 | 9