我有两个有经理和客户的桌子。 所以'客户'表有以下字段:
1. id_client
2. email
3. name
4. id_manager
5. made_a_deal (bool field)
''经理'表有以下字段:
1. id_manager
2. name
这两个表通过id_manager键连接。
我想获得一个表,其中显示以下字段:
1. id_manager
2. managers.name
3. COUNT(By field id_client)
And I couldn't make the fourth filed:
4. COUNT(By field id_client, which have bought something ('client.made_a_deal' is true))
我的代码是:
USE database_name;
SELECT
managers.id_manager,
managers.name,
COUNT(client.id_client) AS `all`
FROM managers
LEFT OUTER JOIN
client
ON
managers.id_manager = client.id_manager
GROUP BY managers.name
;
如何修改代码以达到所需的表格?
答案 0 :(得分:1)
在MySQL中,布尔列实际上是整数,其含义为false
。
所以在SELECT子句中尝试这一行。
SUM(IF client.made_a_deal <> 0, 1, 0)) AS dealmakers,
如果您确定made_a_deal
列中的每个值都是零或一,那么这个更简单的子句就可以了。
SUM(client.made_a_deal) AS dealmakers,