[英]How do I write the following SQL query in Laravel using Eloquent?
[英]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)
附近使用的正确语法 ascat_desc
,SUM(kwh_used)
作为kwh_used
,SUM(cost)
作为cost
' 在第 1 行(SQL:selectMIN(IF(category
。description IS NOT NULL, category
。description, your_electricity_yesterday_category
cat_desc)
作为cat_desc
,SUM(kwh_used)
作为kwh_used
,SUM(cost)
ascost
,your_electricity_yesterday_category
.category_id
fromyour_electricity_yesterday_category
left joincategory
ascategory
onyour_electricity_yesterday_category
.category_id
=category
.id
whereyour_electricity_yesterday_category
.category_id
= 11 andyour_electricity_yesterday_category
.building_id
= 52 andyour_electricity_yesterday_category
.category_id
not in (selectcategory_id
frombuilding_category_settings
wherebuilding_id
= 52 和hide_from_electricity_widget
= 1 gr oup bycategory_id
order bykwh_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.