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

``+--------------------+------------------+ | 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` `
• Mysqli_Query警告：mysqli_query（）期望参数1是mysqli
• 从MySQL查询中返回第n条logging
• closures在php / mysql的警告和错误
• 6000万条目，从某个月份中select条目。 如何优化数据库？
• 为什么在DEFAULT子句中只能有一个CURRENT_TIMESTAMP的TIMESTAMP列？
• MySQL数据库表中的最大logging数
• 重命名MySQL数据库
• 什么是一个成功的MySQL DELETE返回？ 如何检查DELETE是否成功？
• 如何将时间戳转换为MySQL中的date时间？
• MySQL查询select上周的数据？
• 如何在DATE中将DATETIME转换为DATE？