按列分组并显示mysql中所有表的计数

时间:2013-08-28 07:08:56

标签: mysql sql

我有两个表:Table1看起来像这样:

id   type    
1    bike     
2    car      
3    cycle
4    bike

Table2看起来像这样:

id   type    
1    bike     
2    car 

我希望我的最终输出如下所示:

type   count_table1   count_table2
bike        2            1
car         1            1 
cycle       1            0

在SQL中执行此操作的最有效方法是什么?

7 个答案:

答案 0 :(得分:3)

简单的解决方案,无需复杂的表连接和功能:

SELECT type, MAX(count_table1) as count_table1, MAX(count_table2) as count_table2 FROM (
(
    SELECT type, COUNT(*) AS count_table1, 0 AS count_table2
    FROM Table1
    GROUP BY type
) UNION (
    SELECT type, 0 AS count_table1, COUNT(*) AS count_table2
    FROM Table2
    GROUP BY type)
) AS tmp
GROUP BY type

SQL Fiddle

答案 1 :(得分:2)

你可以试试这个:

SELECT t1.TYPE, 
       ifnull(t1.COUNT1,0) CountTable1, 
       ifnull(t2.COUNT2,0) CountTable2 
FROM   (SELECT TYPE, 
               COUNT(*) count1 
        FROM   TABLE1 
        GROUP  BY TYPE)T1 
       LEFT JOIN (SELECT TYPE, 
                         COUNT(*) count2 
                  FROM   TABLE2 
                  GROUP  BY TYPE)T2 
              ON t1.TYPE = t2.TYPE 
UNION 
SELECT t1.TYPE, 
       t1.COUNT1, 
       t2.COUNT2 
FROM   (SELECT TYPE, 
               COUNT(*) count1 
        FROM   TABLE1 
        GROUP  BY TYPE)T1 
       RIGHT JOIN (SELECT TYPE, 
                          COUNT(*) count2 
                   FROM   TABLE2 
                   GROUP  BY TYPE)T2 
               ON t1.TYPE = t2.TYPE 

请参阅SQL Fiddle上的工作示例。

答案 2 :(得分:2)

SELECT a.TYPE, 
       COUNT(a.ID), 
       COUNT(b.ID) 
FROM   TABLE1 AS a 
       LEFT OUTER JOIN TABLE2 AS b 
                    ON a.TYPE = b.TYPE 
GROUP  BY a.TYPE 
UNION 
SELECT b.TYPE, 
       COUNT(a.ID), 
       COUNT(b.ID) 
FROM   TABLE1 AS a 
       RIGHT OUTER JOIN TABLE2 AS b 
                     ON a.TYPE = b.TYPE 
GROUP  BY b.TYPE 

答案 3 :(得分:2)

另一种方法

SELECT a.type, 
       COALESCE(b.type_count, 0) count_table1,
       COALESCE(c.type_count, 0) count_table2
  FROM
(
  SELECT type FROM Table1
  UNION 
  SELECT type FROM Table2
) a LEFT JOIN 
(
  SELECT type, COUNT(*) type_count
    FROM Table1
   GROUP BY type
) b ON a.type = b.type LEFT JOIN
(
  SELECT type, COUNT(*) type_count
    FROM Table2
   GROUP BY type
) c ON a.type = c.type

一些解释:

  • 子查询a获取不同类型的列表(UNION负责处理)。
  • 子查询bc分别计算table1table2中的类型出现次数。
  • 最后,外部SELECT使用LEFT JOINCOALESCE将所有内容整合在一起,用0替换不存在的值。

输出:

|  TYPE | COUNT_TABLE1 | COUNT_TABLE2 |
|-------|--------------|--------------|
|  bike |            2 |            1 |
|   car |            1 |            1 |
| cycle |            1 |            0 |

这是 SQLFiddle 演示

答案 4 :(得分:1)

select type, count(*) from table1 group by type

select type, count(*) from table2 group by type

Getting count of each item

答案 5 :(得分:1)

SELECT a.`TYPE`, 
       COALESCE(tbl1CNT,0) as tbl1CNT,
       COALESCE(tbl2CNT,0) as tbl2CNT
FROM   (SELECT `TYPE` 
        FROM   TABLE1 
        UNION 
        SELECT `TYPE` 
        FROM   TABLE2) a 
       LEFT JOIN (SELECT `TYPE`, 
                         COUNT(*) AS tbl1CNT 
                  FROM   TABLE1 
                  GROUP  BY `TYPE`) b 
              ON a.`TYPE` = b. `TYPE` 
       LEFT JOIN (SELECT `TYPE`, 
                         COUNT(*) AS tbl2CNT 
                  FROM   TABLE2 
                  GROUP  BY `TYPE`) c 
              ON a.`TYPE` = c. `TYPE` 

<强> FIDDLE

答案 6 :(得分:0)

select
    T.type
    ,IFNULL(COUNT(T1.type),0) as 'count_table1'
    ,IFNULL(COUNT(T2.type),0) as 'count_table2'
from
    Table1 as T1
    left join Table2 as T2 on T2.id = T1.id
group by
    T.type

union

select
    T.type
    ,IFNULL(COUNT(T1.type),0) as 'count_table1'
    ,IFNULL(COUNT(T2.type),0) as 'count_table2'
from
    Table2 as T
    left join Table1 as T1 on T1.id = T2.id
group by
    T.type