时间:2018-02-13 01:57:43

标签: mysql sql

我试图让橄榄球比赛的详细信息显示出来,我想要的详细信息包括比赛ID,比赛日期,比赛时间,主队,客队,主场比分,客场比分。 / p>

我需要使用SQL语句来查看匹配日期以计算得分(因为这些年来点数发生了变化)。

以下声明适用于所有以前的评分结构,但是当它到达最新的(当前有一个空的结束日期)时,它会中断。

问题不在于它没有结束日期,因为我已经设置今天的日期如果日期为空,那就是它从info中丢失了日期,我不明白为什么。< / p>

SELECT 
M.MatchID, 
DATE_FORMAT(M.Date, '%d %b %Y'), 
TIME_FORMAT(M.time, '%l:%i %p') AS time,
T1.name, 
T2.name, 
SUM(M.HTries * S.Try) + SUM(M.HConv * S.Conv) + SUM(M.HPT * S.PT) + SUM(M.HPG * S.PG) + SUM(M.HDG * S.DG) AS HScore,
SUM(M.ATries * S.Try) + SUM(M.AConv * S.Conv) + SUM(M.APT * S.PT) + SUM(M.APG * S.PG) + SUM(M.ADG * S.DG) AS AScore,
S.ScoreDateFrom AS ScoreDateFrom,
CASE WHEN NOT S.ScoreDateTo = '0000-00-00' then S.ScoreDateTo ELSE CURDATE() 

END AS ScoreDateTo
FROM 
scoring S, matches M
LEFT JOIN teams T1
    ON T1.TeamID=M.HTeam
LEFT JOIN teams T2
    ON T2.TeamID=M.ATeam
WHERE 
    M.Date >= ScoreDateFrom and M.Date <= ScoreDateTo and M.MatchID = 1;

如果我选择匹配1,这是一个旧的评分结构,我会得到所有正确的信息:

+---------+---------------------------------+---------+-------------+-----------+--------+--------+---------------+-------------+                  
| MatchID | DATE_FORMAT(M.Date, '%d %b %Y') | time    | name        | name      | HScore | AScore | ScoreDateFrom | ScoreDateTo |                  
+---------+---------------------------------+---------+-------------+-----------+--------+--------+---------------+-------------+                  
|       1 | 22 Oct 2016                     | 7:35 PM | New Zealand | Australia |     37 |     10 | 2016-05-22    | 2017-07-31  |                  
+---------+---------------------------------+---------+-------------+-----------+--------+--------+---------------+-------------+ 

但如果我为最近的一场比赛做准备,我就明白了:

+---------+---------------------------------+---------+----------+--------+--------+--------+---------------+-------------+                        
| MatchID | DATE_FORMAT(M.Date, '%d %b %Y') | time    | name     | name   | HScore | AScore | ScoreDateFrom | ScoreDateTo |                        
+---------+---------------------------------+---------+----------+--------+--------+--------+---------------+-------------+                        
|      93 | 11 Feb 2018                     | 3:00 PM | Scotland | France |   NULL |   NULL | NULL          | 2018-02-13  |                        
+---------+---------------------------------+---------+----------+--------+--------+--------+---------------+-------------+ 

包含评分详情的表格如下所示:

+---------+---------------+-------------+------+------+------+------+------+------+---------------------+                                          
| ScoreID | ScoreDateFrom | ScoreDateTo | Try  | PT   | Conv | PG   | DG   | Mark | Update_TS           |                                          
+---------+---------------+-------------+------+------+------+------+------+------+---------------------+                                          
|       1 | 1977-01-01    | 1992-06-30  |    4 |    4 |    2 |    3 |    3 |    0 | 2017-02-08 17:56:39 |                                          
|       2 | 1992-07-01    | 2016-05-13  |    5 |    5 |    2 |    3 |    3 |    0 | 2018-02-13 01:42:29 |                                          
|       3 | 1971-09-01    | 1976-12-31  |    4 |    4 |    2 |    3 |    3 |    3 | NULL                |                                          
|       4 | 2017-08-01    | 0000-00-00  |    5 |    7 |    2 |    3 |    3 |    0 | NULL                |                                          
|       5 | 2016-05-14    | 2016-05-21  |    6 |    8 |    2 |    2 |    3 |    0 | NULL                |                                          
|       6 | 2016-05-22    | 2017-07-31  |    5 |    5 |    2 |    3 |    3 |    0 | NULL                |                                          
+---------+---------------+-------------+------+------+------+------+------+------+---------------------+ 

感谢您是否可以让我知道出了什么问题?

由于

2 个答案:

答案 0 :(得分:2)

您无法引用SELECT子句中WHERE列表中的别名。因此WHERE M.Date >= ScoreDateFrom and M.Date <= ScoreDateTo and M.MatchID = 1;使用实际表格中ScoreDateTo的值,而不是CURDATE()作为调整后的值。您需要在WHERE重复该调整。

WHERE M.Date >= ScoreDateFrom and M.Date <= IF(ScoreDateTo = '0000-00-00', CURDATE(), ScoreDateTo) and M.MatchID = 1;

答案 1 :(得分:0)

您加入比赛和得分的方式。

2018年2月11日的比赛没有相关的得分记录。

因此,当发生连接时,对于该匹配,它将为评分中的任何字段返回NULL。