目前我正在使用 mysql 返回具有相同单词的列中所有数据的总和。为此,我使用以下查询:
SELECT SUM(CASE WHEN status_to = 'Draft' THEN 1 END) AS draft,
SUM(CASE WHEN status_to = 'Unpublish' THEN 1 END) AS unpublish,
SUM(CASE WHEN status_to = 'Publish' THEN 1 END) AS publish,
SUM(CASE WHEN status_to = 'Action' THEN 1 END) AS action,
SUM(CASE WHEN status_to = 'Unlisted' THEN 1 END) AS unlisted,
SUM(CASE WHEN status_to = 'Sold' THEN 1 END) AS sold,
SUM(CASE WHEN status_to = 'Let' THEN 1 END) AS let
FROM `crm_logs`
这会在我的数据库中为我指定的所有术语提供正确的输出,但现在我发现数据库中除了上面指定的变量之外还有更多的状态变量。所以我想要一种方法来拥有与该语句相同的功能,但使状态变量动态化。
基本上,如果这是查询,SUM(CASE WHEN status_to = 'Draft' THEN 1 END) AS draft
则草稿的出现都应该是动态的。