gpt4 book ai didi

mysql - 如何在我的情况下正确使用 Query Builder Laravel?

转载 作者:行者123 更新时间:2023-11-29 07:37:15 25 4
gpt4 key购买 nike

我有一部分 SQL 代码需要为 Query Builder (Laravel) 正确转换。

select
p.*
from posts p, comments c
where c.post_id = p.post_id
group by p.post_id
order by avg(c.mark_first) desc

谁能阻止我? :/

最佳答案

使用查询生成器你可以把它写成

$posts = DB::table('posts as p')
->join('comments as c', 'p.post_id', '=', 'c.post_id')
->groupBy('p.post_id')
->orderByRaw('avg(c.mark_first) desc')
->select('p.*')
->get();

如果你能在 select 部分添加你想要的 groupby 中的所有列会更好,因为如果 mysql 的 full_group_by 上面的查询将变得无效模式已启用,在 5.7+ 中默认启用

关于mysql - 如何在我的情况下正确使用 Query Builder Laravel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48194715/

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