gpt4 book ai didi

php - 如何加入其他表并计算 laravel 中的行数?

转载 作者:行者123 更新时间:2023-11-29 04:34:12 26 4
gpt4 key购买 nike

我正在计算每个求职者有多少职位要求。我有两个表:jobseeker 和 jobposition。

求职者:

+----+---------+-----------+----------------+
| id | fb_name | fullname | desireposition |
+----+---------+-----------+----------------+
| 1 | John | John Cena | 3 |
| 2 | Christ | Christ | 4 |
| 3 | Thomas | Cfitcher | 2 |
+----+---------+-----------+----------------+

职位:

+----+--------+------------------+
| id | job_id | require_position |
+----+--------+------------------+
| 1 | 12 | 3 |
| 2 | 13 | 3 |
| 3 | 14 | 4 |
| 4 | 15 | 5 |
| 5 | 16 | 4 |
| 6 | 17 | 3 |
+----+--------+------------------+

我的预期结果是:

+----+---------+-----------+----------------+-----------------------+
| id | fb_name | fullname | desireposition | total_requireposition |
+----+---------+-----------+----------------+-----------------------+
| 1 | John | John Cena | 3 | 3 |
| 2 | Christ | Christ | 4 | 2 |
| 3 | Thomas | Cfitcher | 2 | 0 |
+----+---------+-----------+----------------+-----------------------+

我想统计每个求职者有多少职位。

这是我尝试使用 crossJoin 的结果,但不确定我实际需要使用哪个连接。

$jobseekers = Jobseeker::crossJoin('jobpositions')
>select('fullname','fb_name','desire_position', DB::raw('count(require_position) as total_requireposition'))
->groupBy('fullname')->paginate(10);

任何人都可以帮助指导我吗?任何帮助将不胜感激。

最佳答案

您想要的常规 MySQL 查询是:

SELECT s.id, fullname, fb_name, desireposition, IFNULL(COUNT(require_position), 0) AS require_position
FROM jobseeker AS s
LEFT JOIN jobposition AS p ON s.desireposition = p.require_position
GROUP BY s.id

我不使用 Laravel,但我认为翻译应该是:

$Jobseeker->select('fullname','fb_name','desire_position', DB::raw('IFNULL(COUNT(require_position), 0) as total_requireposition'))
->leftjoin('jobposition', 'desireposition', '=', 'require_position')
->groupBy('jobseeker.id')
->paginate(10)

关于php - 如何加入其他表并计算 laravel 中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48594184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com