如何在两个不同的表上一起添加两个count(*)结果?

时间:2009-05-05 18:48:46

标签: mysql count sum

我有两张桌子:玩具和游戏。

+--------------------+------------------+
| Field              | Type             |
+--------------------+------------------+
| toy_id             | int(10) unsigned |
| little_kid_id      | int(10) unsigned |
+--------------------+------------------+

+--------------------+------------------+
| Field              | Type             |
+--------------------+------------------+
| game_id            | int(10) unsigned |
| little_kid1        | int(10) unsigned |
| little_kid2        | int(10) unsigned |
| little_kid3        | int(10) unsigned |
+--------------------+------------------+

一个小孩可以有多个玩具。 一个小孩可以一次参加多个游戏。

我想要一个查询,它会给我一个little_kid所涉及的玩具+游戏的总数。

基本上,我想要这两个查询的总和:

SELECT COUNT(*) FROM Toys WHERE little_kid_id = 900;
SELECT COUNT(*) from Games WHERE little_kid1 = 900 
                              OR little_kid2 = 900 
                              OR little_kid3 = 900;

是否可以在单个SQL查询中获取此信息?显然,我可以通过编程方式对它们求和,但这不太可取。

(我意识到这个人为的例子使得架构看起来很无效。让我们假设我们不能改变架构。)

8 个答案:

答案 0 :(得分:111)

包装它们并使用子查询:

SELECT
(SELECT COUNT(*) FROM Toys WHERE little_kid_id = 900)+
(SELECT COUNT(*) from Games WHERE little_kid1 = 900 
                              OR little_kid2 = 900 
                              OR little_kid3 = 900)
AS SumCount

瞧!

答案 1 :(得分:5)

SELECT COUNT(1) FROM
(
    SELECT 1 FROM Toys WHERE little_kid_id = 900
    UNION
    SELECT 1 FROM Games WHERE little_kid1 = 900
                        OR little_kid2 = 900
                        OR little_kid3 = 900
)

答案 2 :(得分:4)

根据此查询可能运行的程度以及数据更改的频率,您可以定期将数据放入聚合表中,如下所示:

CREATE TABLE aggregated (
    little_kid_id INT UNSIGNED,
    games_count INT UNSIGNED,
    toys_count INT UNSIGNED,
    PRIMARY KEY (little_kid_id)
);

性能明智,快速,并避免任何讨厌的子查询。

答案 3 :(得分:4)

试试这个...

db: mysql

SELECT  SUM(dum.tab) AS total FROM (
SELECT COUNT(b.category_id) AS tab FROM tblcategory AS b WHERE b.category_id=1
UNION  ALL
SELECT COUNT(a.category_id) AS tab FROM tblcategory AS a WHERE a.category_id=2
) AS dum

答案 4 :(得分:3)

SELECT
((SELECT COUNT(*) FROM Toys WHERE little_kid_id = 900)+
(SELECT COUNT(*) from Games WHERE little_kid1 = 900 
                              OR little_kid2 = 900 
                              OR little_kid3 = 900))
AS Sum FROM DUAL;

Selecting from the DUAL Table

答案 5 :(得分:0)

  SELECT  M.*,M.TOYSCOUNT+M.GAMECOUNT  
FROM (
    (SELECT COUNT(*) FROM Toys WHERE little_kid_id) AS TOYSCOUNT,
    (SELECT COUNT(*) from Games WHERE little_kid1 = 900 OR little_kid2 = 900 OR little_kid3 = 900) AS GAMECOUNT
    ) M

答案 6 :(得分:0)

select t1.tx,t2.px,t3.mx,t2.px + t3.mx  
        as total from(
SELECT COUNT (DISTINCT id) as tx
FROM Customer) as t1
cross join(
select COUNT (DISTINCT name) as px
FROM details 
) as t2
cross join(
select count (distinct device_id) as mx
from detailconfig 
) as t3

答案 7 :(得分:0)

SELECT COUNT(1) FROM
(
    (SELECT 1 FROM Toys WHERE little_kid_id = 900
    UNION
    SELECT 1 FROM Games WHERE little_kid1 = 900
                        OR little_kid2 = 900
                        OR little_kid3 = 900) as temptable
)