我的代码是
$query = "SELECT teamID, season,
MAX(season) AS current_season,
COUNT(DISTINCT matchID AND season=current_season) AS season_games_played,
FROM finances
WHERE teamID = '$_SESSION[teamID]'";
但这给了我一个错误:(
我想要做的是获得球队本赛季的比赛数量,所以current_season将是列赛季的最大值。
我的下一行代码是找出球队上赛季打了多少场比赛(MAX(赛季)-1)我假设,但这会是怎么写的?
答案 0 :(得分:1)
最简单的方法可能是
SELECT
season,
COUNT(DISTINCT matchID) AS season_games_played
FROM finances
WHERE teamID = '$_SESSION[teamID]' /*<-- Probable SQL injection risk!*/
GROUP BY season
ORDER BY season DESC
LIMIT 2;
我已经从teamID
列表中删除了SELECT
,因为您已经将其传递给了所有人。
我认为'$_SESSION[teamID]'
是php或者什么的。请阅读有关如何避免使用该语言进行SQL注入的内容。
答案 1 :(得分:1)
SELECT
teamID,
season,
COUNT(DISTINCT matchID) AS season_games_played,
FROM finances
WHERE teamID = ?
GROUP BY 1, 2
ORDER BY 2 DESC;
第一排将是最近一个赛季,第二排将是最近一个赛季等。