简体   繁体   中英

SQL command is not giving expected result in Laravel 5.7. Do I need to modify my table or my query?

I have my table data like below photo.

在此处输入图片说明

My expected data should be like this below photo.

在此处输入图片说明

My implemented laravel SQL code is like below.

$img = DB::table('table_name')
->select('table_name.user_id','table_name.post_id','table_name.name')
->groupBy('table_name.user_id')
->get();

This is how I'd do it. This way it's cleaner and working for me.

$img = DB::table('table_name')->select('user_id','post_id','name')
                              ->groupBy('post_id')
                              ->get();
$img = DB::table('table_name')
 ->select('user_id','post_id','name')
 ->groupBy('post_id')
 ->get();

If you're running into this SQLSTATE[42000]: Syntax error or access violation: 1055 error be sure to check that the groupBy parameter is a valid column on the table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM