4

我有两个表: 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 中执行此操作的最有效方法是什么?

4

7 回答 7

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小提琴

于 2013-08-28T07:23:22.873 回答
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负责处理)。
  • 子查询b并分别c计算 和 中的类型出现table1次数table2
  • 最后,一个外部SELECT将它们组合在一起使用LEFT JOINCOALESCE用 替换不存在的值0

输出:

| 类型 | COUNT_TABLE1 | COUNT_TABLE2 |
|-------|-------------|--------------|
| 自行车 | 2 | 1 |
| 汽车 | 1 | 1 |
| 循环 | 1 | 0 |

这是SQLFiddle演示

于 2013-08-28T07:28:28.633 回答
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上的工作示例。

于 2013-08-28T07:17:06.147 回答
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 
于 2013-08-28T07:17:47.757 回答
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` 

小提琴

于 2013-08-28T07:43:47.270 回答
1
select type, count(*) from table1 group by type

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

获取每个项目的计数

于 2013-08-28T07:16:43.783 回答
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
于 2013-08-28T07:17:15.460 回答