MySQL-多行输出情况下不同标准的计数结果

MySQL-多行输出情况下不同标准的计数结果,mysql,sql,subquery,counting,Mysql,Sql,Subquery,Counting,我必须根据XLS导出的4个不同标准获得结果 以下是创建表代码: CREATE TABLE IF NOT EXISTS `relation_tc_agri` ( `tc_id` varchar(255) NOT NULL, `agri_id` varchar(255) NOT NULL, `state` smallint(6) NOT NULL, `send_email` smallint(1) unsigned NOT NULL default '1', KEY `tc_id

我必须根据XLS导出的4个不同标准获得结果

以下是创建表代码:

CREATE TABLE IF NOT EXISTS `relation_tc_agri` (
  `tc_id` varchar(255) NOT NULL,
  `agri_id` varchar(255) NOT NULL,
  `state` smallint(6) NOT NULL,
  `send_email` smallint(1) unsigned NOT NULL default '1',
  KEY `tc_id` (`tc_id`),
  KEY `agri_id` (`agri_id`)
) ENGINE=InnoDB
+-----------+-------------+-------+------------+
| tc_id     | agri_id     | state | send_email |
+-----------+-------------+-------+------------+
| 045bd915c | 2a6ad61aa   | 0     | 1          |
| f50d6b1eb | 413bc4026   | 1     | 1          |
| f50d6b1eb | 1475f4ba4   | 1     | 1          |
| f50d6b1eb | 1475f4ba4   | 2     | 1          |
| gd865be6e | 1475f4ba4   | 2     | 1          |
| d0dbfa93d | 90b3fefcf   | 3     | 0          |
+-----------+-------------+-------+------------+
+-----------+------------+------------------+-----------------+-----------------+
| tc_id     | total rows | rows with state 1| rows with state2| rows with state3|
+-----------+------------+------------------+-----------------+-----------------+
| 045bd915c | 1          | 0                | 0               | 0               |
| f50d6b1eb | 3          | 2                | 1               | 0               |
| gd865be6e | 1          | 0                | 1               | 0               |
| d0dbfa93d | 1          | 0                | 0               | 1               |
+-----------+------------+------------------+-----------------+-----------------+
和一些示例(为了演示而稍微修改):

CREATE TABLE IF NOT EXISTS `relation_tc_agri` (
  `tc_id` varchar(255) NOT NULL,
  `agri_id` varchar(255) NOT NULL,
  `state` smallint(6) NOT NULL,
  `send_email` smallint(1) unsigned NOT NULL default '1',
  KEY `tc_id` (`tc_id`),
  KEY `agri_id` (`agri_id`)
) ENGINE=InnoDB
+-----------+-------------+-------+------------+
| tc_id     | agri_id     | state | send_email |
+-----------+-------------+-------+------------+
| 045bd915c | 2a6ad61aa   | 0     | 1          |
| f50d6b1eb | 413bc4026   | 1     | 1          |
| f50d6b1eb | 1475f4ba4   | 1     | 1          |
| f50d6b1eb | 1475f4ba4   | 2     | 1          |
| gd865be6e | 1475f4ba4   | 2     | 1          |
| d0dbfa93d | 90b3fefcf   | 3     | 0          |
+-----------+-------------+-------+------------+
+-----------+------------+------------------+-----------------+-----------------+
| tc_id     | total rows | rows with state 1| rows with state2| rows with state3|
+-----------+------------+------------------+-----------------+-----------------+
| 045bd915c | 1          | 0                | 0               | 0               |
| f50d6b1eb | 3          | 2                | 1               | 0               |
| gd865be6e | 1          | 0                | 1               | 0               |
| d0dbfa93d | 1          | 0                | 0               | 1               |
+-----------+------------+------------------+-----------------+-----------------+
我有四个不同的状态(0、1、2和3),并且必须计算每个tc_id的每个状态有多少个结果

预期输出:

CREATE TABLE IF NOT EXISTS `relation_tc_agri` (
  `tc_id` varchar(255) NOT NULL,
  `agri_id` varchar(255) NOT NULL,
  `state` smallint(6) NOT NULL,
  `send_email` smallint(1) unsigned NOT NULL default '1',
  KEY `tc_id` (`tc_id`),
  KEY `agri_id` (`agri_id`)
) ENGINE=InnoDB
+-----------+-------------+-------+------------+
| tc_id     | agri_id     | state | send_email |
+-----------+-------------+-------+------------+
| 045bd915c | 2a6ad61aa   | 0     | 1          |
| f50d6b1eb | 413bc4026   | 1     | 1          |
| f50d6b1eb | 1475f4ba4   | 1     | 1          |
| f50d6b1eb | 1475f4ba4   | 2     | 1          |
| gd865be6e | 1475f4ba4   | 2     | 1          |
| d0dbfa93d | 90b3fefcf   | 3     | 0          |
+-----------+-------------+-------+------------+
+-----------+------------+------------------+-----------------+-----------------+
| tc_id     | total rows | rows with state 1| rows with state2| rows with state3|
+-----------+------------+------------------+-----------------+-----------------+
| 045bd915c | 1          | 0                | 0               | 0               |
| f50d6b1eb | 3          | 2                | 1               | 0               |
| gd865be6e | 1          | 0                | 1               | 0               |
| d0dbfa93d | 1          | 0                | 0               | 1               |
+-----------+------------+------------------+-----------------+-----------------+
使用以下方法计算每个tc_id的总行数(状态0、1、2和3)没有问题:

COUNT(*) AS "total rows"
但当涉及到计数状态为1、2或3的行时,我会遇到错误。我尝试了几个连接,甚至子查询(这是糟糕且缓慢的),以消除“子查询返回多个结果”消息等

如果有人能让我走上正确的道路,我将不胜感激;)

提前多谢

干杯


Em.

您可以通过条件聚合来实现这一点。在MySQL中,这看起来像:

select tc_id, count(*) as totalrows,
       sum(state = 1) as state_1,
       sum(state = 2) as state_2,
       sum(state = 3) as state_3
from relation_tc_agri
group by tc_id;

MySQL将布尔值视为数字,true为
1
,false为
0
。只需求和就可以计算出该值为真的次数。

这是一个很好的技巧。我总是这样解决这些问题:SUM(如果(state=1,1,0))作为state_1,但从现在起,我将始终使用您的建议Great Gordon!我尝试了许多不同的方法,但没有想到这一点。它工作完美,感谢您快速高效的回答;)