【问题标题】:MySQL: Filter Table and count multiple columnsMySQL:过滤表并计算多列
【发布时间】:2018-08-31 08:57:52
【问题描述】:

我有这个(SQL)表:

dataId  userId  type
104     1   customer
105     5   interessted

现在我想要一个查询来为每个用户获取每种类型存在多少条目。

例如,如果用户 ID 为 10 的用户有 10 个类型为“customer”的条目和 5 个类型为“interressted”的条目。

userID    customers    interrested
10        10           5

我该怎么做?

【问题讨论】:

标签: mysql sql


【解决方案1】:

你似乎想要条件聚合:

select userid, 
       sum( type = 'customer' ) as customers, 
       sum( type = 'interessted' ) as interessted
from table t
where userid = 10
group by userid;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多