我正在开发一个项目,如果架构或查询需要进行一些更改或者完全需要更改,我希望您快速查看。
该项目旨在为羽毛球队创建一个排名系统,其排名将基于锦标赛中的得分规则
我首先创建了以下表格
球员
+----------+-------------------------------------+------+-----+--------------
| Field | Type | Null | Key | +----------+-------------------------------------+------+-----+--------------
| id | int(11) | NO | PRI |
| name | varchar(250) | NO | |
| image | text | YES | |
| plays | enum('RH','LH') | NO | |
| added_on | datetime | NO | |
| status | enum('active','inactive','retired') | NO | | +----------+-------------------------------------+------+-----+--------------
小组
+------------+----------------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(150) | NO | UNI | NULL | |
| image | text | YES | | NULL | |
| status | enum('active','in-active') | NO | | active | |
| added_on | datetime | NO | | CURRENT_TIMESTAMP | |
| updated_on | datetime | YES | | NULL | |
+------------+----------------------------+------+-----+-------------------+----------------+
球员加入队伍
+-----------+---------------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| player_id | int(11) | NO | MUL | NULL | |
| team_id | int(11) | NO | | NULL | |
| status | enum('active','inactive') | NO | | NULL | |
| added_on | datetime | NO | | CURRENT_TIMESTAMP | |
+-----------+---------------------------+------+-----+-------------------+----------------+
赛
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| year | int(4) | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+
匹配
+---------------+---------------------------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------------------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| team_one | int(11) | NO | MUL | NULL | |
| team_two | int(11) | NO | | NULL | |
| winner_id | int(11) | NO | | NULL | |
| tournament_id | int(11) | NO | MUL | 1 | |
| added_on | datetime | NO | | CURRENT_TIMESTAMP | |
| match_type | enum('pool','quarter','semi','final') | NO | | pool | |
| sets | smallint(2) | NO | | 1 | |
+---------------+---------------------------------------+------+-----+-------------------+----------------+
匹配分数
+----------+-------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| match_id | int(11) | NO | MUL | NULL | |
| team_id | int(11) | NO | MUL | NULL | |
| set_num | enum('1','2','3') | NO | | NULL | |
| score | smallint(2) | NO | | NULL | |
| added_on | datetime | NO | | CURRENT_TIMESTAMP | |
+----------+-------------------+------+-----+-------------------+----------------+
池
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | UNI | NULL | |
| tournament_id | int(11) | NO | MUL | NULL | |
+---------------+-------------+------+-----+---------+----------------+
有一件事你会注意到我没有在任何地方保存排名得分,我在运行时使用以下查询计算
SELECT
T.id, T.name, T.status,
IFNULL(T.image,'no-image.png') as DP,
(SELECT COUNT(*)
FROM badminton_matches M
WHERE (M.team_one = T.id OR M.team_two = T.id)) as played,
(SELECT COUNT(*)
FROM badminton_matches M
WHERE M.winner_id = T.id) as won,
(SELECT COUNT(*)
FROM badminton_matches M
WHERE ((M.team_one = T.id OR M.team_two = T.id)
AND (M.winner_id != T.id))) as lost,
(
((SELECT COUNT(*)
FROM badminton_matches M
WHERE M.winner_id = T.id) * 2) +
(((SELECT COUNT(*)
FROM badminton_matches M
WHERE (M.match_type = 'quarter'
AND M.winner_id = T.id)) * 2) + 1) +
(((SELECT COUNT(*)
FROM badminton_matches M
WHERE (M.match_type = 'semi'
AND M.winner_id = T.id)) * 2) + 2) +
(((SELECT COUNT(*)
FROM badminton_matches M
WHERE (M.match_type = 'final' AND M.winner_id = T.id)) * 2) + 5)) as Points
FROM
badminton_teams T
ORDER BY
(Points) DESC;
首先
使用查询或
是否应保存更新
或
编辑:
将查询更新为以下
SELECT
T.id, T.name, T.status,
IFNULL(T.image,'no-image.png') as DP,
(SELECT COUNT(*)
FROM badminton_matches M
WHERE ((M.team_one =T.id or M.team_two = T.id) and M.winner_id IS NOT NULL)) as played,
(SELECT COUNT(*)
FROM badminton_matches M
WHERE M.winner_id=T.id) as won, (SELECT COUNT(*)
FROM badminton_matches M
WHERE ((M.team_one =T.id or M.team_two = T.id) AND (M.winner_id!=T.id))) as lost,
((SELECT (SUM(BMS.points_won)-SUM(BMS.points_lost))
FROM
badminton_match_score BMS
JOIN badminton_matches M ON (M.id=BMS.match_id)
where M.team_one=T.id OR M.team_two=T.id and M.winner_id is not null)/(SELECT COUNT(*)
FROM badminton_matches M
WHERE ((M.team_one =T.id or M.team_two = T.id) and M.winner_id IS NOT NULL))) AS AVG_SCORE,
(
((SELECT COUNT(*)
FROM badminton_matches M
WHERE M.winner_id=T.id)*2) +
(SELECT COUNT(*)
FROM badminton_matches M
WHERE (M.match_type='quarter' AND M.winner_id=T.id))
+
((SELECT COUNT(*)
FROM badminton_matches M
WHERE (M.match_type='semi' AND M.winner_id=T.id))*2)
+
((SELECT COUNT(*)
FROM badminton_matches M
WHERE (M.match_type='final' AND M.winner_id=T.id))*5)
)
as Points
FROM badminton_teams T
order by (Points) DESC, lost ASC, AVG_SCORE DESC
答案 0 :(得分:2)
使用查询
计算运行时评分是否正确
如果您希望它是实时应用程序。在我看来,是的,如果积分没有累积,将在比赛后重置。对于Sql性能问题。
每次我在数据库中保存匹配结果时,我应该保存更新吗
更新仅用于修改数据,在匹配历史记录后保存数据就足够了。
我应该为此目的安排一个cron工作
如果在更大的数据库和生产力方面,是的。