如果我能得到这个答案,我已经广泛搜索了一个特定的案例,但在我看来它应该有效。但事实并非如此。
我基本上想将数据连接到我的数据库中的一行中。简单地说,如果没有现有数据,它会将文件名变量注入(inject)到行中。
如果行中有现有数据,我需要在开头附加一个“,”,以便稍后在我的页面上使用 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是string,$planid是int。
关于php - 如果现有行为空mysql,如何使用特定的UPDATE查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26859085/