gpt4 book ai didi

mysql - 将 Select 语句转换为另一个 select To Eloquant

转载 作者:行者123 更新时间:2023-11-30 22:30:31 26 4
gpt4 key购买 nike

如何将这样的 SQL 查询转换为 Eloquent 或 QueryBuilder :

SELECT * FROM studies
where studies.id in(SELECT study_id FROM (
SELECT max(studies.end_date), studies.id as study_id
from workers inner join resumes on workers.id=resumes.worker_id
inner join studies on resumes.id=studies.resume_id where
resumes.title="main" group by workers.id) as SQ2

或全局

例如,我们如何从其他带有 eloquant 的 select 语句中进行选择:

SELECT a.id from (SELECT * FROM A INNER JOIN B ON a.id=b.id where a.id > 10) as SUBQ1

最佳答案

我认为您当前的查询在所有情况下都没有获得最大结束日期的 study_id,您可以试试这个:

SELECT * FROM studies
where studies.end_date in (
SELECT max(studies.end_date)
from workers inner join resumes on workers.id=resumes.worker_id
inner join studies on resumes.id=studies.resume_id where
resumes.title="main" group by workers.id)

如果你想用 laravel Eloquant 实现它,你可以这样做:

$result = DB::table('studies')
->select("*")
->whereIn('end_date', function($query){
$query->selectRaw("max(studies.end_date) as max_date")
->from('workers')
->join('resumes', 'workers.id', '=', 'resumes.worker_id')
->join('studies', 'resumes.id', '=', 'studies.resume_id')
->where('resumes.title', '=', 'main')
->groupBy('workers.id');
})
->get();
foreach($result as $row) {
print_r($row);
}

关于mysql - 将 Select 语句转换为另一个 select To Eloquant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016354/

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