我实际上有一个匹配的表,id,team1,team2,date 如果team1是比赛的胜利者,我试图获得一支球队在过去十场比赛中取得多少胜利。
我设法以这种方式获得球队的最后十场比赛,但我找不到获得胜利数的方法
SELECT *
FROM matches
WHERE time1 = 13 OR time2 = 13
ORDER BY data DESC, id ASC
LIMIT 10
答案 0 :(得分:3)
试试这个:
SELECT team1, COUNT(team1) as 'wins'
FROM matches
WHERE team1 = 13 or team2 = 13
GROUP BY team1
LIMIT 10
它应该给你像
这样的结果team1 | wins
===== ====
Bears 5
Tigers 3
Cubs 2
答案 1 :(得分:2)
http://www.w3schools.com/sql/sql_func_count.asp
SELECT time1, COUNT(team1) AS 'wins'
FROM matches
WHERE time1 = 13 OR time2 = 13
ORDER BY data DESC, id ASC
LIMIT 10
GROUP BY wins
我建议为获胜者添加一个单独的专栏,但我认为这就是你正在寻找的......并且拼出正确的队伍。
答案 2 :(得分:1)
可能是这样的:
SELECT COUNT(time1)
FROM matches
WHERE time1 = 13 OR time2 = 13
ORDER BY data DESC, id ASC
GROUP BY time1 //GROUP BY WINNER TEAM
LIMIT 10
答案 3 :(得分:1)
使用COUNT
。顺便说一句,您的“日期”列实际上是“日期”吗?
SELECT count(*)
FROM matches
WHERE time1 = 13
这将返回给定团队的胜利数量。如果您需要结果表,请使用Brian's answer:
SELECT time1, COUNT(*) AS wins
FROM matches
GROUP BY time1
ORDER BY 2 DESC