对于我使用的查询,我希望它输出最新的日期,但是,表格确实是混合的。例如,我在表中使用以下四个条目:
由于兼容性问题,我没有使用MatchDate行的日期索引。它似乎有效,但在子查询中使用MIN()
函数时,它将停止使用WHERE
过滤器。我希望它使用过滤器向我显示接下来要做的任何行(下一步是什么匹配),但它不应该在过去。
SELECT ID, HomeTeamID, AwayTeamID, MatchDate, MatchTime FROM Matches
WHERE HomeTeamID = 1 OR AwayTeamID = 1
AND MatchDate = (SELECT MatchDate FROM Matches
WHERE MatchDate >= CURRENT_DATE()
LIMIT 1)
LIMIT 1
答案 0 :(得分:0)
SELECT ID, HomeTeamID, AwayTeamID, MatchDate, MatchTime FROM Matches WHERE HomeTeamID = 1 OR AwayTeamID = 1 AND MatchDate >= CURRENT_DATE() ORDER BY MatchDate DESC LIMIT 1 OFFSET 1
试试这个
OR
SELECT ID, HomeTeamID, AwayTeamID, MatchDate, MatchTime FROM Matches WHERE HomeTeamID = 1 OR AwayTeamID = 1 AND MatchDate >= CURRENT_DATE() ORDER BY MatchDate ASC LIMIT 1 OFFSET 1
最近日期视图
SELECT ID, HomeTeamID, AwayTeamID, MatchDate, MatchTime FROM Matches WHERE HomeTeamID = 1 OR AwayTeamID = 1 AND MatchDate > CURRENT_DATE() ORDER BY MatchDate DESC LIMIT 1 OFFSET 1
答案 1 :(得分:0)
这是您的查询:
SELECT ID, HomeTeamID, AwayTeamID, MatchDate, MatchTime FROM Matches
WHERE HomeTeamID = 1 OR AwayTeamID = 1
AND MatchDate = (SELECT MatchDate FROM Matches
WHERE MatchDate >= CURRENT_DATE()
LIMIT 1)
LIMIT 1
这个查询在做什么?
LIMIT 1
)(不是ORDER BY
)。HomeTeamID = 1 OR AwayTeamID = 1 AND MatchDate = <some future date>
。您正在混合AND
和OR
而不使用括号,因此AND
优先于OR
。这意味着您拥有:HomeTeamID = 1 OR (AwayTeamID = 1 AND MatchDate = <some future date>)
。这可能是您想要的也可能不是。LIMIT 1
没有ORDER BY
)。也许你想要这个:
SELECT ID, HomeTeamID, AwayTeamID, MatchDate, MatchTime
FROM Matches
WHERE (HomeTeamID = 1 OR AwayTeamID = 1)
AND MatchDate = (SELECT MatchDate FROM Matches
WHERE MatchDate >= CURRENT_DATE()
ORDER BY MatchDate
LIMIT 1);
与
相同SELECT ID, HomeTeamID, AwayTeamID, MatchDate, MatchTime
FROM Matches
WHERE (HomeTeamID = 1 OR AwayTeamID = 1)
AND MatchDate = (SELECT MIN(MatchDate) FROM Matches
WHERE MatchDate >= CURRENT_DATE());
然而,这将为您提供下一个预定日期,无论哪支球队将参加比赛。那么你正在寻找一个队伍1将参加的那个日期的比赛。你可能会找到这样的比赛。
所以也许你想要这个:
SELECT ID, HomeTeamID, AwayTeamID, MatchDate, MatchTime
FROM Matches
WHERE ID = (SELECT ID
FROM Matches
WHERE (HomeTeamID = 1 OR AwayTeamID = 1) AND MatchDate >= CURRENT_DATE()
ORDER BY MatchDate
LIMIT 1);
可以写得更简单
SELECT ID, HomeTeamID, AwayTeamID, MatchDate, MatchTime
FROM Matches
WHERE (HomeTeamID = 1 OR AwayTeamID = 1) AND MatchDate >= CURRENT_DATE()
ORDER BY MatchDate
LIMIT 1;
这将寻找将来最接近的球队1比赛。如果不存在将来的团队1匹配,则不会返回任何行。
关于你的其他尝试:
查询1:
SELECT ID, HomeTeamID, AwayTeamID, min(MatchDate)
FROM Matches
WHERE HomeTeamID = 1 OR AwayTeamID = 1 AND DATE(MatchDate) >= CURRENT_DATE()
LIMIT 1
GROUP BY ID
这会导致错误,但MySQL可能会在LIMIT
之前编写GROUP BY
。您选择所有记录WHERE HomeTeamID = 1 OR (AwayTeamID = 1 AND DATE(MatchDate) >= CURRENT_DATE())
。这些是按ID分组的,它完全没有任何作用,因为每条记录都应该有唯一的ID。然后,您选择ID, HomeTeamID, AwayTeamID, min(MatchDate)
min(MatchDate)
只是MatchDate
,因为我们正在查看单个记录。然后你限制为一个没有ORDER BY
子句的记录,所以在你发现的所有记录中你都选择了一个。
查询2:
SELECT ID, HomeTeamID, AwayTeamID, min(MatchDate)
FROM Matches
WHERE HomeTeamID = 1 OR AwayTeamID = 1 AND DATE(MatchDate) >= DATE(CURRENT_DATE())
您选择所有记录WHERE HomeTeamID = 1 OR (AwayTeamID = 1 AND DATE(MatchDate) >= CURRENT_DATE())
。然后,您选择所有这些记录中的min(MatchDate)
。将返回一行(或零),因为您聚合了所有行(没有GROUP BY
子句)。您也可以选择ID, HomeTeamID, AwayTeamID
。这是无效的,因为您正在聚合,所以哪个ID,应该显示找到的记录的主队和客队?根据会话设置,MySQL可以轻松选择一个ID
,一个HomeTeamID
和AwayTeamID
任意选择或抛出错误(应该如此)。