laravel 5 inner join and group by query syntax error

蓝咒 提交于 2019-12-11 05:18:02

问题


please help me with my format query in laravel 5. I am a beginner in using laravel 5, and i just search to came up with this query, but now i am getting error i dont know where and why? i think my error is only in syntax?

Here is my code:

$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);

any suggestions? i printed the error and it says this :

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

回答1:


You have extra starting [ in this line:-

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

It need to be (use DB::raw() to use 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);

Note:-

Regarding the error you get (Syntax error or access violation: 1055) check this answer and do accordingly:-

aravel : Syntax error or access violation: 1055 Error



来源:https://stackoverflow.com/questions/46641312/laravel-5-inner-join-and-group-by-query-syntax-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!