admin

laravel 5内部联接和按查询语法分组

sql

请帮助我在laravel 5中进行格式查询。我是使用laravel
5的初学者,我只是搜索提出此查询,但现在却遇到错误,我不知道在哪里以及为什么?我认为我的错误仅在语法上?

这是我的代码:

$date = date("Y-m-d", strtotime($request->datepicker2));

$products =  DB::table('shipping_table')
->select('products.product_name', 'products.price', 'Sum(shipping_products.quantity) AS qtysold', 'shipping_table.sold_date')
->join('shipping_products','shipping_table.shipping_id', '=', 'shipping_products.shipping_id')
->join('products','products.product_id', '=', 'shipping_products.product_id')
->where([['DATE(shipping_table.sold_date)', '=',$date], ['shipping_table.shipping_status', '=' ,1]])
->groupBy('products.product_name')
->paginate(8);

有什么建议?我打印了错误,它说:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DATE(shipping_table.sold_date)' in 'where clause'

阅读 144

收藏
2021-07-01

共1个答案

admin

[在这一行有更多的起点:-

->where([['DATE(shipping_table.sold_date)', '=',$date, ['shipping_table.shipping_status', '=' ,1]])

它必须是(DB::raw()用于mysql functions):-

$date = date("Y-m-d", strtotime($request->datepicker2));

$products =  DB::table('shipping_table')
->select('products.product_name', 'products.price', DB::raw("Sum(shipping_products.quantity) as qtysold"), 'shipping_table.sold_date')
->join('shipping_products','shipping_table.shipping_id', '=', 'shipping_products.shipping_id')
->join('products','products.product_id', '=', 'shipping_products.product_id')
->where([[DB::raw("date(shipping_table.sold_date)"),$date], ['shipping_table.shipping_status', '=' ,1]])
->groupBy('products.product_name')
->paginate(8);

笔记:-

关于得到的错误(Syntax error or access violation: 1055),请检查此答案并相应地执行以下操作:

aravel:语法错误或访问冲突:1055错误

2021-07-01