Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.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中为每个客户查找type1和type2事务的计数_Sql_Mysql_Pivot Table - Fatal编程技术网

如何在mysql中为每个客户查找type1和type2事务的计数

如何在mysql中为每个客户查找type1和type2事务的计数,sql,mysql,pivot-table,Sql,Mysql,Pivot Table,我有一张客户表: id name 1 customer1 2 customer2 3 customer3 和一个事务表: id customer amount type 1 1 10 type1 2 1 15 type1 3 1 15 type2 4 2 60 type2 5 3 23

我有一张客户表:

id   name
1    customer1
2    customer2
3    customer3
和一个事务表:

id   customer   amount   type
1    1          10       type1
2    1          15       type1
3    1          15       type2
4    2          60       type2
5    3          23       type1
我希望查询返回的是下表

name        type1    type2
customer1   2        1
customer2   0        1
customer3   1        0
这表明customer1进行了两次类型为1的事务处理和类型为2的事务处理,依此类推

是否有一个查询我可以用来获得这个结果,或者我必须使用程序代码

SELECT  name, 
        (
        SELECT  COUNT(*)
        FROM    transaction t
        WHERE   t.customer = c.id
                AND t.type = 'type1'
        ) AS type1,
        (
        SELECT  COUNT(*)
        FROM    transaction t
        WHERE   t.customer = c.id
                AND t.type = 'type2'
        ) AS type2
FROM    customer c
要对这些列应用
WHERE
条件,请使用以下命令:

SELECT  name
FROM    (
        SELECT  name, 
                (
                SELECT  COUNT(*)
                FROM    transaction t
                WHERE   t.customer = c.id
                        AND t.type = 'type1'
                ) AS type1,
                (
                SELECT  COUNT(*)
                FROM    transaction t
                WHERE   t.customer = c.id
                        AND t.type = 'type2'
                ) AS type2
        FROM    customer c
        ) q
WHERE   type1 > 3
你可以试试

select c.id as customer_id
   , c.name as customer_name
   , sum(case when t.`type` = 'type1' then 1 else 0 end) as count_of_type1
   , sum(case when t.`type` = 'type2' then 1 else 0 end) as count_of_type2
from customer c
   left join `transaction` t
   on c.id = t.customer
group by c.id, c.name
此查询只需在联接上迭代一次。

dirk抢先一步;)

类似,但在mysql 4.1中也可以使用

Select c.name,
sum(if(type == 1,1,0)) as `type_1_total`,
sum(if(type == 2,1,0)) as `type_2_total`,
from 
customer c
join transaction t on (c.id = t.customer)
;

是的,这是一个很好的解决方案,但是如果有许多类型,是否有更短的查询?因为我正在一个巨大的数据网络中处理许多类型,您需要的是所谓的数据透视。不,在
MySQL
中没有简单的方法。你确定需要将这些类型作为列而不是行吗?是的,将它们作为行将使一切变得更简单,但不一定需要它们,因为suchIt似乎无法在WHERE子句中使用新列
@andho
:将整个查询包装到子查询中并从中选择。使用
IF(t.type='type1',1,0)
更简洁,但+1因为这是实现它的方法,事实上,因为MySQL中的“布尔”实际上只有1和0,你甚至不需要IF/CASE语句:
SUM(t.type='type1')
就可以了。但是,即使有了几年的C经验,当我看到像
some_array[x==1]
..:-)这样的代码时,我还是觉得有点不舒服但是我同意
IF
这一点,因为它的详细程度稍微低一点,'case-when'部分不会只检查事务表中许多列中的一列,以获取customer@andho:对不起,我不明白你的问题。
交易
表中是否有多个可以找到客户ID的列?如果是这样的话,在你的帖子中没有任何建议。