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

How do I add two count(*) results together on two different tables?(如何在两个不同的表上添加两个 count(*) 结果?)
本文介绍了如何在两个不同的表上添加两个 count(*) 结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

+--------------------------------+-----------------+|领域 |类型 |+--------------------------------+-----------------+|玩具ID |int(10) 无符号 ||little_kid_id |int(10) 无符号 |+--------------------------------+-----------------++--------------------------------+-----------------+|领域 |类型 |+--------------------------------+-----------------+|游戏ID |int(10) 无符号 ||小孩子1 |int(10) 无符号 ||小孩子2 |int(10) 无符号 ||小孩子3 |int(10) 无符号 |+--------------------------------+-----------------+

一个小孩可以有多个玩具.一个小孩可以同时参加多个游戏.

我想要一个查询,它会给我一个 little_kid 参与的玩具和游戏的总数.

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

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

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

(我意识到人为设计的示例使架构看起来效率低下.假设我们无法更改架构.)

解决方案

包装它们并使用子查询:

选择(从 little_kid_id = 900 的玩具中选择计数(*))+(SELECT COUNT(*) from Games WHERE little_kid1 = 900或 little_kid2 = 900或 little_kid3 = 900)AS SumCount

瞧!

I have two tables: Toys and Games.

+--------------------+------------------+
| 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 |
+--------------------+------------------+

A little kid can have multiple toys. A little kid can be participating in multiple games at once.

I want a query that will give me the total number of toys + games that a little_kid is involved with.

Basically, I want the sum of these two queries:

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;

Is it possible to get this in a single SQL query? Obviously, I can sum them programmatically, but that's less desirable.

(I realize that the contrived example makes the schema look ineffecient. Let's assume that we can't change the schema.)

解决方案

Wrap them up and use subqueries:

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

Voila!

这篇关于如何在两个不同的表上添加两个 count(*) 结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)