【问题标题】:Count and Grouping 2 column in a table对表中的 2 列进行计数和分组
【发布时间】:2017-05-17 11:52:44
【问题描述】:

这是我的表格和数据

id | owner | buyer
1    1       3
2    2       2
3    1       2

我希望结果是这样的

user | totals
2      3
1      2
3      1

用户字段表示所有者和买方。 希望大家谅解。 谢谢~

【问题讨论】:

  • 所有者 UNION ALL 买家,GROUP BY 其结果。

标签: sql group-by sql-order-by


【解决方案1】:

您可以使用union allgroup by 来做到这一点:

select user, count(*)
from ((select owner as user from t
      ) union all
      (select buyer from t
      )
     ) ob
group by user
order by user;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多