【问题标题】:How to count distinct elements from different tables in mysql?如何计算mysql中不同表的不同元素?
【发布时间】:2015-08-19 12:35:46
【问题描述】:

这是table_1

d1
--------
a  
b  
c  
d  
e  

table_2

d2
--------
a  
b  
d  

table_3

d3
--------
b  
c  

预期的新表是上述所有表中 a、b、c、d、e 的计数之和

final_table

a   2  
b   3  
c   2  
d   2  
e   1

【问题讨论】:

    标签: mysql database


    【解决方案1】:

    你可以做一个简单的表联合和这样的计数

    select id, count(id)
    from(
      select id from d1
      union all
      select id from d2
      union all
      select id from d3
    ) t
    group by id
    

    SAMPLE FIDDLE

    【讨论】:

    • 非常感谢。我一直在尝试这样做很长时间....上帝保佑你!
    • @saurabh 欢迎您!感谢您的快速接受!
    猜你喜欢
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多