76

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

+--------------------+------------------+
| 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 参与的玩具和游戏的总数。

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

从 little_kid_id = 900 的玩具中选择计数(*);
从游戏中选择 COUNT(*) little_kid1 = 900
                              或 little_kid2 = 900
                              或 little_kid3 = 900;

是否有可能在单个 SQL 查询中得到它?显然,我可以以编程方式对它们求和,但这不太理想。

(我意识到这个人为的例子使模式看起来效率低下。假设我们不能改变模式。)

4

8 回答 8

166

将它们包装起来并使用子查询:

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

瞧!

于 2009-05-05T18:50:30.517 回答
9
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;

从 DUAL 表中选择

于 2018-02-16T10:15:24.470 回答
7
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
)
于 2009-05-05T18:52:05.677 回答
5

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

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

性能方面,它会很快就很热,并避免任何讨厌的子查询。

于 2009-05-05T23:06:53.317 回答
3

试试这个...

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
于 2012-11-08T09:33:55.290 回答
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
于 2016-12-30T10:55:03.910 回答
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
)
于 2020-05-04T18:35:41.830 回答
-1
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
于 2019-04-12T04:03:33.327 回答