gpt4 book ai didi

mysql - 使用 Laravel 语法的 Sql leftJoin 查询显示错误

转载 作者:行者123 更新时间:2023-11-29 10:52:42 26 4
gpt4 key购买 nike

这是我用于左连接的查询,它工作正常,但当我将其转换为 Laravel 语法时缺少一些内容。

要转换的查询是

$result = DB::select("select amenities.name as 
name,amenities.type_id,amenities.id as id, amenities.icon, rooms.id as status
from amenities left join rooms on find_in_set(amenities.id, rooms.amenities)
and rooms.id = $room_id and type_id !=4");

我正在做这个

$result = DB::table('amenities')
->select('amenities.name as name', 'amenities.type_id' , 'amenities.id as id'
, 'amenities.icon', 'rooms.id as status' )
->leftJoin('rooms', function ($join) {
$join->on('FIND_IN_SET(amenities.id, rooms.amenities)')
->where('rooms.id' , '=', '$room_id')
->where('type_id','!=', 4);
})->get();

错误是

InvalidArgumentException in F:\xampp\htdocs\arheb\Arheb\vendor\laravel\framework\src\Illuminate\Database\Query\JoinClause.php line 79: Not enough arguments for the on clause.

最佳答案

您的查询有误。我假设 amenities.idrooms.amenities 分别是 amenitiesrooms 表的属性。

MySQL FIND_IN_SET() returns the position of a string if it is present (as a substring) within a list of strings.

您需要在 on() 函数的第一个和第二个参数中传递列名称。

$result = DB::table('amenities')
->select('amenities.name as name', 'amenities.type_id' , 'amenities.id as id'
, 'amenities.icon', 'rooms.id as status' )
->leftJoin('rooms', function ($join) {
$join->on('amenities.id', '=', 'rooms.amenities')
->where('rooms.id' , '=', '$room_id')
->where('type_id','!=', 4);
})->get();

关于mysql - 使用 Laravel 语法的 Sql leftJoin 查询显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43452530/

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