簡體   English   中英

我如何在 laravel 中編寫以下查詢

[英]How do i write the following Query in laravel

我有這個查詢,我正在嘗試執行

$other = $this->electricityConnections->select('category_id')
    ->from('building_category_settings')
    ->where('building_id', '=', 52)
    ->where('hide_from_electricity_widget', '=', 1)
    ->groupBy('category_id')
    ->orderBy('kwh_used', 'desc');

$electCategory = $this->electricityConnections
    ->addselect(('MIN(IF(category.description IS NOT NULL, 
    category.description, your_electricity_yesterday_category.cat_desc) 
            as cat_desc'),
        ('SUM(kwh_used) as kwh_used'), ('SUM(cost) as cost'),
        'your_electricity_yesterday_category.category_id')
    ->leftJoin('category as category',
        'your_electricity_yesterday_category.category_id', '=', 'category.id')
    ->where('your_electricity_yesterday_category.category_id', '=', 11)
    ->where('your_electricity_yesterday_category.building_id', '=', 52)
    ->whereNotIn('your_electricity_yesterday_category.category_id', $other)
    ->get();
dd($electCategory);

但我一直收到這個錯誤

"message": "SQLSTATE[42000]: Syntax error or access violation: 1064 您的 SQL 語法有誤;請查看與您的 MySQL 服務器版本對應的手冊,以了解在 ' cat_desc)附近使用的正確語法 as cat_descSUM(kwh_used)作為kwh_usedSUM(cost)作為cost ' 在第 1 行(SQL:select MIN(IF(categorydescription IS NOT NULL, categorydescription, your_electricity_yesterday_category cat_desc)作為cat_descSUM(kwh_used)作為kwh_usedSUM(cost) as cost , your_electricity_yesterday_category . category_id from your_electricity_yesterday_category left join category as category on your_electricity_yesterday_category . category_id = category . id where your_electricity_yesterday_category . category_id = 11 and your_electricity_yesterday_category . building_id = 52 and your_electricity_yesterday_category . category_id not in (select category_id from building_category_settings where building_id = 52 和hide_from_electricity_widget = 1 gr oup by category_id order by kwh_used desc))",

當我對上面的代碼執行toSql()時,我在下面得到以下內容

"select `MIN(IF(category`.`description IS NOT NULL, category`.`description, your_electricity_yesterday_category`.`cat_desc)` as `cat_desc`, `SUM(kwh_used)` as `kwh_used`, `SUM(cost)` as `cost`, `your_electricity_yesterday_category`.`category_id` from `your_electricity_yesterday_category` left join `category` as `category` on `your_electricity_yesterday_category`.`category_id` = `category`.`id` where `your_electricity_yesterday_category`.`category_id` = ? and `your_electricity_yesterday_category`.`building_id` = ? and `your_electricity_yesterday_category`.`category_id` not in (select `category_id` from `building_category_settings` where `building_id` = ? and `hide_from_electricity_widget` = ? group by `category_id` order by `kwh_used` desc)"

我究竟做錯了什么?

我認為主要問題是addSelect部分。 您缺少MIN的結尾) 當所選列包含 sql 函數時,嘗試使用DB::raw

addselect(
    DB::raw('MIN(IF(category.description IS NOT NULL, category.description, your_electricity_yesterday_category.cat_desc)) as cat_desc'),
    DB::raw('SUM(kwh_used) as kwh_used'),
    DB::raw('SUM(cost) as cost'), 
    'your_electricity_yesterday_category.category_id'
)

您缺少下一行末尾的括號

MIN(IF(category.description IS NOT NULL, category.description, your_electricity_yesterday_category.cat_desc)) as cat_desc 

您需要在cat_desc之前關閉括號,這似乎是語法問題。

您可以使用selectRaw ,查看文檔: https://laravel.com/docs/9.x/queries#selectraw

->selectRaw("
    MIN(IF(category.description IS NOT NULL, category.description, your_electricity_yesterday_category.cat_desc)) as cat_desc),
    SUM(kwh_used) as kwh_used,
    SUM(cost) as cost), 
    your_electricity_yesterday_category.category_id               
")

. 是SQL中的關鍵字,不加引號不可以作為列名使用。 在 MySQL 中,列名之類的東西使用反引號引用,即 . 與``

就個人而言,我不會打擾; 我只是重命名該列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM