![](/img/trans.png)
[英]How can I fetch the number of rows from a database table and insert the total rows into columns of a different table using UPDATE query
[英]I need to query 1 table to obtain the total number of rows pertaining to a value in a second and third table
我有3张桌子:
user_subscriptions
ID | SUB_ID | TYPE | Type 1 = scripts | Type 2 = packages
1 | 1 | 1 |
2 | 2 | 1 |
3 | 3 | 1 |
4 | 4 | 1 |
5 | 1 | 2 |
6 | 2 | 2 |
7 | 3 | 2 |
8 | 4 | 2 |
9 | 5 | 1 |
10 | 6 | 2 |
11 | 7 | 1 |
12 | 8 | 1 |
13 | 9 | 2 |
剧本
ID | AUTHOR_ID |
1 | 58 |
2 | 58 |
3 | 58 |
4 | 58 |
5 | 52 |
6 | 53 |
7 | 58 |
8 | 55 |
9 | 58 |
10 | 56 |
包装
ID | AUTHOR_ID |
1 | 58 |
2 | 58 |
3 | 58 |
4 | 58 |
5 | 56 |
6 | 57 |
7 | 58 |
8 | 58 |
9 | 56 |
10 | 58 |
在“ user_subscriptions”表中,“ SUB_ID”列对应于脚本ID或程序包ID,具体取决于为该订阅设置的类型。
我的目标是查询特定作者创建的脚本和软件包的订阅总数。 作者58已创建6个脚本和7个程序包(总共13个),但是只有9个与author_id 58相关的用户订阅。
我在编写查询时遇到麻烦,该查询将返回与author_id 58有关的user_subscriptions。
任何帮助都将不胜感激。 我正在从PHP进行此查询,并且可能会使用mysqli_num_rows来获取所需的计数。 再次感谢!
SELECT author_id
, COUNT(*) total
FROM
( SELECT id, author_id FROM scripts
UNION ALL
SELECT id, author_id FROM packages
) x
GROUP
BY author_id;
请注意,此解决方案中的单词比上面的webnoobs评论要少;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.