条件上的Mysql计数记录,选择了多个字段

时间:2012-09-26 20:12:15

标签: mysql count case

我尝试使用计数案例来计算某一列中的记录(runs.Finish),如果它们等于一个。

当我在下面的第三行添加计数案例时,我似乎无法使其工作。

SELECT
  `jockeys`.`JockeyInitials`,
  `jockeys`.`JockeySurName` ,
  COUNT(  `runs`.`JockeysID` ), 
  COUNT( CASE WHEN `runs`.`Finish`=1 THEN 1 ELSE NULL END ) AS `runs`.`Finish` , 
  SUM(  `runs`.`StakeWon` ) 
FROM runs, jockeys
WHERE  `runs`.`JockeysID` =  `jockeys`.`JockeysID` 
  AND  `runs`.`JockeysID` >=1
  AND  `runs`.`JockeysID` <=100
GROUP BY  `runs`.`JockeysID`

1 个答案:

答案 0 :(得分:0)

您的查询应该是(按案例列表列的列和别名检查)

SELECT
  `jockeys`.`JockeyInitials`,
  `jockeys`.`JockeySurName` ,
  COUNT(  `runs`.`JockeysID` ), 
  COUNT( CASE WHEN `runs`.`Finish`=1 THEN 1 ELSE NULL END ) AS `Finish` , 
  SUM(  `runs`.`StakeWon` ) 
FROM runs, jockeys
WHERE  `runs`.`JockeysID` =  `jockeys`.`JockeysID` 
  AND  `runs`.`JockeysID` >=1
  AND  `runs`.`JockeysID` <=100
GROUP BY  `jockeys`.`JockeyInitials`,
  `jockeys`.`JockeySurName`