mysql - 如何在 laravel 中将这个复杂的 mysql 查询作为原始查询运行
问题描述
我有一个 mysql 查询,当我在 phpmyadmin 中运行这个查询时工作得很好。原始的 sql 查询是
SELECT IF(COUNT(u.id) > 1
, GROUP_CONCAT(CONCAT(a.amenity_name, '_' ,a.id)), a.amenity_name) as m_concat
, u.id as unit_id
, u.building_id
, (uav.id) as uav_id
, a.*
FROM amenities a
JOIN amenity_values av
ON a.id = av.amenity_id
JOIN units_amenities_values uav
ON av.id = uav.amenity_value_id
JOIN units u ON u.id = uav.unit_id
where a.category_id = 370
AND a.property_id = 82
AND u.building_id = 1265
group
by u.id
order
by u.id asc
但是,当我尝试在 laravel 中将确切的查询作为原始查询运行时,它会抛出错误。我正在尝试将其运行为:
$statement = DB::select(DB::raw("SELECT IF(COUNT(u.id) > 1, GROUP_CONCAT(CONCAT(a.amenity_name, '_' ,a.id)), a.amenity_name) as m_concat, u.id as unit_id, u.building_id, (uav.id) as uav_id, a.* FROM amenities a INNER JOIN amenity_values av ON a.id = av.amenity_id INNER JOIN units_amenities_values uav ON av.id = uav.amenity_value_id INNER JOIN units u ON u.id = uav.unit_id where a.category_id = '370' AND a.property_id = '82' AND u.building_id = '1265' group by u.id order by u.id asc"));
在运行上面的确切语句时,它会引发以下错误:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'amenity_db_prod.a.amenity_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: SELECT IF(COUNT(u.id) > 1, GROUP_CONCAT(CONCAT(a.amenity_name, '_' ,a.id)), a.amenity_name) as m_concat, u.id as unit_id, u.building_id, (uav.id) as uav_id, a.* FROM amenities a INNER JOIN amenity_values av ON a.id = av.amenity_id INNER JOIN units_amenities_values uav ON av.id = uav.amenity_value_id INNER JOIN units u ON u.id = uav.unit_id where a.category_id = '370' AND a.property_id = '82' AND u.building_id = '1265' group by u.id order by u.id asc)
解决方案
推荐阅读
- javascript - Mongoose/MongoDb FindOneAndUpdate 未按预期工作
- android - Android构建成功但无法解析R
- angular - 无法从 Ionic 的登录页面路由到菜单页面
- botframework - 在 QnA maker 中更改答案布局
- c++ - 在发布模式下使用 C++ 中的 VS2017 构建的应用程序不会因未处理的异常而中断
- python - 如何根据熊猫数据框中的多列值条件排除行?
- python - 在 scipy.stats.chisquare 中处理 NaN
- python - 如何使用 django 上的按钮 id 在引导模式上显示数据库中的动态内容
- powershell - 如何通过在命令行中执行脚本来使用 Powershell 获取 INI 节文件?
- c - 如何执行 .c 文件?