我在下面列出了我所运行的查询以及预期的和实际的结果。我认为这个问题与WHERE子句中过滤器的组合有关-我希望一个表中的前两个查询的结果基本上是两列。
预期输出:28;实际产量:28
SELECT COUNT(t.Id)
FROM teams AS t
WHERE teamStatusId = 339 AND live = 1
预期输出:1;实际输出:1
SELECT COUNT(p.Id)
FROM Players AS p
INNER JOIN teams AS t ON t.TeamId = p.Id
WHERE teamStatusId = 339 AND isInjuired = 1
预期输出28 | 1个实际输出:30 | 30
SELECT
COUNT(p.Id) AS 'Players',
COUNT(t.Id) AS 'Team'
FROM
Players AS p
INNER JOIN
Teams AS t ON p.TeamId = t.Id
WHERE
teamStatusId = 339
AND p.isInjuired = 1
AND t.live = 1
答案 0 :(得分:0)
如果一个球员属于多支球队,那么您的盘点次数将会增加,在这种情况下,您需要在DISTINCT
中使用COUNT()
:
SELECT COUNT(DISTINCT p.Id) AS 'Players', COUNT(DISTINCT t.Id) AS 'Team'
FROM Players as p INNER JOIN
Teams t
ON p.TeamId = t.Id
WHERE teamStatusId = 339 AND p.isInjuired = 1 AND t.live = 1;