简体   繁体   中英

Using , separated in concat function in mysql query

Using this query, i will append the userid in table but it will insert in continuous i want to separated by , plz suggest change in query

 DB::table('table_user_create_activity')
               ->where(['activity_id'=>$query_get_activity->activity_id])
              ->update(['accepted_join_id'=>DB::raw('CONCAT(ifnull(accepted_join_id,""),'.$get_user_id.')')]);

The problem is in your DB::raw SQL statement. You are using MySQL concat , but that method does not add a separator. For adding a separator use concat_ws (ws = with separator) and then the first argument is your separator.

->update(['accepted_join_id'=>DB::raw("CONCAT_WS(',', ifnull(accepted_join_id,''),".$get_user_id.')')])

A sidenote, your query is open for sql injections because of $get_user_id is added in the DB::raw . To close the sql injection change it to the following.

->update(['accepted_join_id'=>DB::raw("CONCAT_WS(',', ifnull(accepted_join_id,''),?)", [$get_user_id])])

This way we escape the user id before adding it in the query.

Hope This solves Your Problem

DB::table('table_user_create_activity')
    ->where([ 'activity_id' => $query_get_activity->activity_id ])
    ->update([
        'accepted_join_id' => DB::raw("CONCAT(ifnull(accepted_join_id,''), ',' , $get_user_id)")
    ]);

DB::table('table_user_create_activity')->where(['activity_id'=>$query_get_activity->activity_id])->update(['accepted_join_id'=>DB::raw('CONCAT(ifnull(NULL,accepted_join_id) ,'.$get_user_id.')')]);

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