如何在mysql中查找每个客户的type1和type2的交易计数

发布于 2024-08-05 02:26:48 字数 562 浏览 3 评论 0原文

我有一个客户表:

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 的交易和 1 笔类型 2 的交易,依此类推。

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

I have a customer table:

id   name
1    customer1
2    customer2
3    customer3

and a transaction table:

id   customer   amount   type
1    1          10       type1
2    1          15       type1
3    1          15       type2
4    2          60       type2
5    3          23       type1

What I want my query to return is the following table

name        type1    type2
customer1   2        1
customer2   0        1
customer3   1        0

Which shows that customer1 has made two transactions of type1 and 1 transaction of type2 and so forth.

Is there a query which I can use to obtain this result or do I have to use procedural code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

命硬 2024-08-12 02:26:48

您可以尝试

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

此查询只需在连接上迭代一次。

You could try

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

This query needs to iterate only once over the join.

小苏打饼 2024-08-12 02:26:48

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)
;

dirk beat me to it ;)

Similar but will work in mysql 4.1 too.

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)
;
调妓 2024-08-12 02:26:48
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  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

To apply WHERE conditions to these columns use this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文