Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
同一列的mysql计数,但有两个计数_Mysql_Sql - Fatal编程技术网

同一列的mysql计数,但有两个计数

同一列的mysql计数,但有两个计数,mysql,sql,Mysql,Sql,我正在尝试对同一个专栏进行计数,但它的状态不同,我怎么做呢?我试过了,但我遇到了很多问题 SELECT distinct tblsmsgroups.GroupName, tblsmsphones.PhoneNumber, tblsmsphones.GroupID, count(case when status = 1 then) as active FROM tblsmscustomers, tblsmsgroups INNER JOIN tbls

我正在尝试对同一个专栏进行计数,但它的状态不同,我怎么做呢?我试过了,但我遇到了很多问题

SELECT
    distinct tblsmsgroups.GroupName,
    tblsmsphones.PhoneNumber,
    tblsmsphones.GroupID,
    count(case when status = 1 then) as active
FROM
    tblsmscustomers,
    tblsmsgroups
INNER JOIN tblsmsphones 
    ON tblsmsgroups.groupID = tblsmsphones.GroupID 

状态=1,然后激活多少,状态=0,然后计数多少,我仍然停留在语法上的差异,我该怎么做,有什么想法吗?

你可能想要这样的东西:

SELECT 
    tblsmsgroups.GroupName,
    tblsmsphones.PhoneNumber,
    tblsmsphones.GroupID,
    sum(status = 1) as active_count,
    sum(status != 1) as inactive_count
FROM
    tblsmscustomers ,
    tblsmsgroups
    INNER JOIN tblsmsphones ON tblsmsgroups.groupID = tblsmsphones.GroupID
GROUP BY GroupName, PhoneNumber, GroupID 

好吧,如果你想添加内容,你需要一个
分组依据

如果要连接表,则需要
join
——切勿使用逗号

这意味着:

SELECT g.GroupName, p.PhoneNumber, p.GroupID,
       SUM(status = 1) as active,
       SUM(status = 0) as not_active
FROM tblsmsgroups g JOIN
     tblsmsphones p
     ON g.groupID = p.GroupID JOIN
     tblsmscustomers c
     ON c.customerID = p.customerID   -- have to guess the JOIN condition here
GROUP BY g.GroupName, p.PhoneNumber, p.GroupID; 

注意:您的问题没有足够的信息来理解三个表之间的连接条件;以上是一个合理的猜测。

在MySQL中,您可以只
SUM(status=1)
,混合连接样式可能会导致问题我没有想到连接,它出现在原始问题中。不过,关于总和表达式的观点很正确。欢迎来到SO。请看