gpt4 book ai didi

mysql - 将 mysql 查询转换为 Eloquent 查询问题

转载 作者:行者123 更新时间:2023-11-29 05:05:09 24 4
gpt4 key购买 nike

我在 mysql 中构建了以下查询。并尝试了很多转换为 laravel 但没有成功。

SELECT u.id, u.purchase_item_name,u.sales_item_name, us.type,
GROUP_CONCAT(p.itemcode) AS purchase_items,
GROUP_CONCAT(s.itemcode) as sales_items
FROM composite_inventories as u
LEFT JOIN composite_has_inventories as us ON u.id = us.composite_inventory_id
LEFT JOIN inventories as s ON US.inventory_id = s.id AND us.type='sale'
LEFT JOIN inventories as p ON US.inventory_id = p.id AND us.type='purchase'
GROUP BY u.id

我正在尝试在 laravel datatabel 查询中进行上述查询,但它给出了如下错误。

SQLSTATE[42000]:语法错误或访问冲突:1055 'saas.composite_inventories.purchase_item_name' 不在 GROUP BY 中(SQL:选择 GROUP_CONCAT(p.itemcode)作为 purchase_items,GROUP_CONCAT(s.itemcode) as sales_items, composite_inventories . id , composite_inventories . purchase_item_name , composite_inventories . sales_item_name , us . type from composite_inventories left join composite_has_inventories as us on composite_inventories . id = us . composite_inventory_id left join inventories as s on US . inventory_id = s . id and us . type = 'sale' left join inventories as p on US . inventory_id = p . id and us . type = 'purchase' group by composite_inventories . id )

我试过下面的 laravel 查询。

$result = Compositeinventory::select([
DB::raw('GROUP_CONCAT(p.itemcode) as purchase_items'),
DB::raw('GROUP_CONCAT(s.itemcode) as sales_items'),
'composite_inventories.id',
'composite_inventories.purchase_item_name',
'composite_inventories.sales_item_name',
'us.type'
])->leftJoin('composite_has_inventories as us', 'composite_inventories.id', '=', 'us.composite_inventory_id')
->leftJoin('inventories as s', function($join)
{
$join->on('US.inventory_id', '=', 's.id');
$join->on('us.type','=',DB::raw("'sale'"));
})
->leftJoin('inventories as p', function($join)
{
$join->on('US.inventory_id', '=', 'p.id');
$join->on('us.type','=',DB::raw("'purchase'"));
})
->groupBy('composite_inventories.id')->get();

我刚刚试过下面的查询

$row = DB::table('composite_inventories as u')->select([
'u.id',
'u.purchase_item_name',
'u.sales_item_name',
DB::raw('GROUP_CONCAT(p.itemcode) as purchase_items'),
DB::raw('GROUP_CONCAT(s.itemcode) as sales_items')
])->leftJoin('composite_has_inventories as us', 'u.id', '=', 'us.composite_inventory_id')
->leftJoin('inventories as s', function($join)
{
$join->on('US.inventory_id', '=', 's.id');
$join->on('us.type','=',DB::raw("'sale'"));
})
->leftJoin('inventories as p', function($join)
{
$join->on('US.inventory_id', '=', 'p.id');
$join->on('us.type','=',DB::raw("'purchase'"));
})
->groupBy('u.id', 'u.purchase_item_name','u.sales_item_name');

以上查询在显示数据表时有效。但是当我尝试搜索过滤器时,它会给出以下错误,因为数据库中不存在 purchase_items 和 sales_items 字段,但它们只是别名。错误是

Exception Message:↵↵SQLSTATE[42000]: Syntax error or access violation: 1583 Incorrect parameters in the call to native function 'LOWER' (SQL: select count  as aggregate from (select u.id, u.purchase_item_name, u.sales_item_name, GROUP_CONCAT(p.itemcode) as purchase_items, GROUP_CONCAT(s.itemcode) as sales_items from composite_inventories as u left join composite_has_inventories as us on u.id = us.composite_inventory_id left join inventories as s on US.inventory_id = s.id and us.type = 'sale' left join inventories as p on US.inventory_id = p.id and us.type = 'purchase' where (LOWER(composite_inventories as u.purchase_item_name) LIKE %a% or LOWER(composite_inventories as u.purchase_items) LIKE %a% or LOWER(composite_inventories as u.sales_item_name) LIKE %a% or LOWER(composite_inventories as u.sales_items) LIKE %a%) group by u.id, u.purchase_item_name, u.sales_item_name) count_row_table)

最佳答案

这不是 laravel 的问题,您的查询无效,因为它包含一些未聚合且未在组子句中提及的列。要更正此问题,您需要将 group by 子句更新为

group by u.id, u.purchase_item_name,u.sales_item_name, us.type

在查询生成器中

->groupBy('composite_inventories.id', 'composite_inventories.purchase_item_name','composite_inventories.sales_item_name', 'composite_has_inventories.type')

如果每个 composite_inventories 有多个 type(s),那么我想您也需要 GROUP_CONCAT 并从 group by 子句中删除此列。

语法错误更新,您没有使用lower功能正确只需传递您要将其值转换为小写的列名,如

WHERE (
LOWER(u.purchase_item_name) LIKE '% a %'
OR LOWER(u.purchase_items) LIKE '% a %'
OR LOWER(u.sales_item_name`) LIKE '% a %'
OR LOWER(u.sales_items) LIKE '% a %'
)

如果您的数据库排序规则未设置为区分大小写,则无需使用 lower()有关更多信息的功能,请查看 B.5.4.1 Case Sensitivity in String Searches

此外,如果您在查询中使用 mysql 函数,您可能需要 raw() laravel 查询生成器的方法

关于mysql - 将 mysql 查询转换为 Eloquent 查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934666/

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