MySQL查询来计算一列中的公用数

时间:2018-07-31 09:17:36

标签: mysql sql

我有一个主表作为第一个图像,我需要产生输出作为第二个表。谁能帮我做mysql查询。

主表

enter image description here

输出

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以进行有条件的聚合:

select anumber,
       sum(tsp = 'aplace') as aplace,
       sum(tsp = 'bplace') as bplace,
       sum(tsp = 'cplace') as cplace
from table t
group by anumber;

答案 1 :(得分:1)

尝试使用CASE WHEN

   select anumber,
           sum(case when tsp = 'aplace' then 1 else 0 end) as aplace,
           sum(case when tsp = 'bplace' then 1 else 0 end) as bplace,
           sum(case when tsp = 'cplace' then 1 else 0 end) as cplace,
    from table t
    group by anumber;