这个:
$stockCodes = \DB::connection('sage_accounts')
->table('sales_order_items')
->select('stock_code')
->from('sales_order_items')
->orderBy('stock_code')
->distinct()->get();
返回任何具有 RDxxx-xx-H7-xx 股票代码格式的股票代码,但这是不正确的格式。正确的格式是 RDxxx-Hx-xx-xx。该查询不会返回该格式的股票代码。以销售订单行项目 11649 为例,其库存代码为 RD0448-H7-09-28。它不会与其余股票代码一起返回,并且:
$stockCodes = \DB::connection('sage_accounts')
->table('sales_order_items')
->select('stock_code')
->from('sales_order_items')
->where('id', 11649)
->orderBy('stock_code')
->distinct()->get();
返回空数据集。虽然这样:
select distinct `stock_code` from `sales_order_items`
where `id` = 11649 order by `stock_code` asc
返回正确的数据
为什么查询生成器不会返回股票代码格式为 RDBxxxx-Hxx 的项目?
最佳答案
你可以使用这个 DB::select('your_query');
DB::select('select distinct stock_code from sales_order_items
where id = 11649 order by stock_code asc');
关于laravel - 带破折号的奇怪 Laravel 查询生成器行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40870966/