gpt4 book ai didi

mysql - 如何将此 SQL 查询转换为 Laravel 语法?

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

这是我的 MySQL 查询(在 phpMyAdmin 中工作):

    SELECT waktu_kerusakan, workcenter, COUNT(workcenter) AS jumlah_repair 
FROM repair WHERE year(waktu_kerusakan)='2019' GROUP BY workcenter ,
month(waktu_kerusakan) BETWEEN 1 and 6 Order By jumlah_repair Desc

然后,我尝试像下面这样使用 Laravel 语法(不起作用):

    $sql = Main::groupBy('workcenter')->select('workcenter', \DB::raw('count(*) as frekuensi'))
->whereYear('waktu_kerusakan', 'like', "%".$tahun."%")
->OrderBy('frekuensi', 'Desc')
->groupBy(\DB::raw("MONTH(waktu_kerusakan)"), [1, 6])
->get();

请任何人帮助我将 MySQL 查询转换为 Laravel 语法。谢谢!

最佳答案

试试这个。

    $repair = DB::table('repair')
->select('waktu_kerusakan','workcenter', DB::raw('COUNT(workcenter) AS jumlah_repair'))
->whereYear('waktu_kerusakan', $tahun)
->groupBy(DB::raw("MONTH(waktu_kerusakan)"), 'workcenter')
->whereIn(DB::raw('MONTH("waktu_kerusakan")'),[1, 6])
->orderBy('frekuensi', 'DESC')
->get()

关于mysql - 如何将此 SQL 查询转换为 Laravel 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58741452/

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