如何在两个不同的表格上添加两个计数（*）结果？

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

``` SELECT COUNT（*）FROM WHERE little_kid_id = 900;
SELECT COUNT（*）from WHERE little_kid1 = 900
或little_kid2 = 900
OR little_kid3 = 900;
```

（我意识到这个人为的例子使模式看起来无效，我们假设我们不能改变模式。）

5 Solutions collect form web for “如何在两个不同的表格上添加两个计数（*）结果？”

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

` `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 )` `

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

` `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` `
` ` 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` `
• 何时使用STRAIGHT_JOIN和MySQL
• Python导入MySQLdb错误 - Mac 10.6
• 我如何修改mysql表中列的大小？
• 如何更改数据库，表，列的sorting规则？
• 使用JDBC和MySQL运行.sql脚本
• CakePHP - 获取最后的查询运行
• 如何从MySQL数据库中删除重复的条目？
• JDBC批量插入性能
• 仅当值不存在时才返回行
• 使用不同数据库的数据库和模式之间的差异？
• ON子句中的MySQL未知列