Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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
Javascript 如何对多个条件的多列求和并在一个条件中显示_Javascript_Mysql_Sql_Node.js - Fatal编程技术网

Javascript 如何对多个条件的多列求和并在一个条件中显示

Javascript 如何对多个条件的多列求和并在一个条件中显示,javascript,mysql,sql,node.js,Javascript,Mysql,Sql,Node.js,我有一个这样的表,现在我需要通过tpi获取所有数据组 +---------------------+---------+----------------+ | trxDate | trxType | tpi | +---------------------+---------+----------------+ | 2018-06-28 00:00:00 | Sent | SI0005 | | 2018-07-02 00:00:

我有一个这样的表,现在我需要通过tpi获取所有数据组

+---------------------+---------+----------------+
| trxDate             | trxType | tpi            |
+---------------------+---------+----------------+
| 2018-06-28 00:00:00 | Sent    | SI0005         |
| 2018-07-02 00:00:00 | Sent    | SI0005         |
| 2018-07-04 00:00:00 | Sent    | SI0005         |
| 2018-05-25 00:00:00 | Open    | SI0007         |
| 2018-06-26 00:00:00 | Open    | SI0007         |
| 2018-05-25 00:00:00 | Sent    | SI0007         |
| 2018-06-23 00:00:00 | Sent    | SI0007         |
+---------------------+---------+----------------+
我需要打开和发送的记录

+---------------------+---------+----------------+
| tpi                 | open    | sent           |
+---------------------+---------+----------------+
|  SI0005             | 0       | 3              |
|  SI0007             | 2       | 2              |   
+---------------------+---------+----------------+
我尝试了一些子查询,但没有得到想要的响应

SELECT  tblcount.tpi, tblcount.qType, count(tblcount.id) total FROM (SELECT id, tpi, 'Open' qType FROM `tbl`WHERE trxType = 'Open' UNION SELECT id, tpi, 'Sent' qType FROM `tbl` WHERE trxType = 'Sent') tblcount GROUP BY  tblcount.third_party_id, tblcount.qType
您可以执行条件聚合:

select tpi,
       sum(trxType = 'Open') as trxType_open, 
       sum(trxType = 'Sent') as trxType_Sent
from tbl t
group by tpi;
您可以执行条件聚合:

select tpi,
       sum(trxType = 'Open') as trxType_open, 
       sum(trxType = 'Sent') as trxType_Sent
from tbl t
group by tpi;

只需使用条件聚合:

select tpi,
       sum( (trx_type = 'open') ) as num_open,
       sum( (trx_type = 'sent') ) as num_sent
from tbl
group by tpi;

这使用了一个MySQL扩展,该扩展将布尔表达式视为整数上下文中的整数,其中“1”表示true,“0”表示false。

只需使用条件聚合:

select tpi,
       sum( (trx_type = 'open') ) as num_open,
       sum( (trx_type = 'sent') ) as num_sent
from tbl
group by tpi;
这使用了一个MySQL扩展,该扩展将布尔表达式视为整数上下文中的整数,其中“1”表示true,“0”表示false