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


how to use a specific UPDATE query if existing row is empty mysql

我已经广泛搜索了一个特定的案例,如果我能得到这个答案,但我认为它应该有效。但事实并非如此。

我基本上想将数据连接到数据库中的一行中。简单地说,如果没有现有数据,它会将文件名变量注入行中。

如果行中有现有数据,我需要在开头附加一个",",这样我就可以稍后在页面上使用php来分解文件名。

这是我的东西,似乎不起作用。

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

尝试这个

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

请确保$filenames为字符串,$planid是int。

您可以这样做:

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