如果现有行为空mysql,如何使用特定的UPDATE查询

I have searched far and wide for a specific case were i could get this answer but what i have seems to me like it should work. but it doesn't.

I basically want to concatenate data into a row in my database. Put simply if there is no existing data it injects the filenames variable into the row.

If there is existing data in the row i need to append a "," to the start so that i can break the filenames out later with php on my page.

Here's what i have that doesn't seem to work.

  $query  = "UPDATE plan 
             SET plan_files = if(plan_files is null,concat(plan_files,
                   '{$filenames}'),concat(plan_files, ',{$filenames}')) 
             WHERE plan_id='{$planid}'";

try this

$query = "update plan  SET plan_files = (case when plan_files IS NULL then '".$filenames."' else concat(plan_files , ',".$filenames."') end ) where plan_id =". $planid;

Please make sure that $filenames is string and $planid is int.

You can do like this :

    "UPDATE `plan` SET 
     plan_files=CASE
     WHEN (plan_files='') THEN  '".$filenames."'
     ELSE concat(name,',".$filenames."')
     END
     WHERE plan_id='".$planid."'"