我有以下表格:
表用户
| id | profile_img |
--------------------
| 21| name.img|
| 22| name.img|
| 33| name.img|
表 user_translations
|id | user_id |first_name |
--------------------------
| 1| 21 | panais |
| 2| 22 | andreas|
| 3| 33 | minas |
表关于
| id | user_id |
----------------
| 1| 33 |
| 2| 22 |
| 3| 21 |
table 上的东西
| id | user_id |
----------------
| 1| 33 |
| 2| 22 |
| 3| 21 |
| 4| 21 |
| 5| 33 |
每个用户都可以写入
about articles to
about table
and thing articles tothings table
我想获取所有用户的文章总数。换句话说,我想从上面的 user_translations
表中获取 minas 总共写了 3 篇文章。
感谢任何帮助。
已更新
我已经发布了答案
最佳答案
我无法帮助您使用 Laravel 的查询构建器,但在纯 mysql 中,您需要的查询类似于:
SELECT ut.`firstname`,ut.`lastname`,COUNT( DISTINCT(a.`id`) ) + COUNT( DISTINCT(t.`id`) ) as total_articles
FROM `user_translations` ut
INNER JOIN `about` a ON a.`user_id`=ut.`user_id`
INNER JOIN `things` t ON t.`user_id`=a.`user_id`
GROUP BY ut.`user_id`
所以也许你可以使用DB::raw
并得到你想要的
关于mysql - 对不同表中的多列进行计数和求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38722602/