我当前正在使用Lumen 5.7作为我的后端API。我需要使用左联接从多个表中选择一些字段。 我有以下查询(普通mysql查询)
SELECT issue_id,issue_receipt_id, date_format(issue_datetime, '%d-%m-%Y') as issue_datetime,
issue_item,sum(issue_wt) as issue_wt, round(avg(issue_pure),3) as issue_pure,
sum(issue_fine) as issue_fine, sum(issue_cash) as issue_cash,
sum(receipt_wt) as receipt_wt, sum(receipt_fine) as receipt_fine,
pure_name as issue_item_name
FROM item_issues
LEFT JOIN receipt_items ON receipt_id = issue_receipt_id
LEFT JOIN purities ON pure_id = issue_item
group by issue_receipt_id;
我尝试像这样在管腔中
$receipts = DB::table('item_issues')
->leftJoin('receipt_items', 'receipt_items.receipt_id', '=', 'item_issues.issue_receipt_id')
->leftJoin('purities', 'purities.pure_id', '=', 'item_issues.issue_item')
->select('issue_id', 'issue_receipt_id', DB::raw("date_format(issue_datetime, '%d-%m-%Y') as issue_datetime"), 'issue_item', 'pure_name', DB::raw('sum(issue_wt) as issue_wt'),
DB::raw('round(avg(issue_pure),3) as issue_pure'),
DB::raw('sum(issue_fine) as issue_fine'),
DB::raw('sum(issue_cash) as issue_cash'),
DB::raw('sum(receipt_wt) as receipt_wt'),
DB::raw('sum(receipt_fine) as receipt_fine'))
->groupBy('item_issues.issue_receipt_id', 'item_issues.issue_item')->get();
但是它产生诸如语法错误或访问冲突之类的错误:1055 SELECT列表的表达式#1不在GROUP BY子句中,并且包含未聚合的列'item_issues.issue_id',该列在功能上不依赖于GROUP BY子句中的列; 但是我必须选择以上字段,如何做到这一点而不会出现问题。而且我尝试禁用严格模式,但是无论在.env还是其他文件中,我都不知道在何处禁用此选项。我在流明中找不到database.config。任何人都可以为此提供帮助。
答案 0 :(得分:0)
默认情况下,Lumen应用程序中没有任何名为'config/database.php'
的文件夹。但可以像在laravel中一样将此配置文件添加到您的管腔应用中。
并将以下代码的和平度粘贴到您的database.php文件中。
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'strict' => false
]
]
]
这里重要的是'strict' => false
模式。