mySQL计数字符串的出现次数

时间:2011-03-28 21:00:38

标签: mysql

我有这个问题......

SELECT SUM(brownlow_votes) AS votes, 
player_id, 
player_name, 
player_team, 
COUNT(*) AS vote_count 
FROM afl_brownlow_phantom, afl_playerstats 
WHERE player_id=brownlow_player 
AND brownlow_match=player_match 
GROUP BY player_id 
ORDER BY votes DESC LIMIT 50

因此“投票”成为玩家拥有的投票数,“vote_count”成为玩家投票的次数(匹配)。这很好。

但是,我有另一个名为“brownlow_lock”的列,它是空白或“Y”。如何获得'Y'的出现次数?我知道我可以解决这个问题,将它改为0或1,只是做一个SUM(),但我不想去编辑插入数据的大量页面。

2 个答案:

答案 0 :(得分:1)

如果我理解正确你就需要添加

COUNT(CASE WHEN brownlow_lock='Y' THEN 1 END) AS Cnt

到您的查询

答案 1 :(得分:0)

尝试使用IF control flow function

  

SELECT SUM(IF(brownlow_lock ='Y',1,0))lock_count ...