How do I add two count(*) results together on two different tables?(如何在两个不同的表上将两个 count(*) 结果相加?)
问题描述
我有两张桌子:玩具和游戏.
是否可以在单个 SQL 查询中获得此信息?显然,我可以以编程方式对它们求和,但这不太理想.
(我意识到人为的示例使架构看起来效率低下.假设我们无法更改架构.)
把它们包起来并使用子查询:
瞧!
I have two tables: Toys and Games.
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:
Voila!
这篇关于如何在两个不同的表上将两个 count(*) 结果相加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!