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

标签 php mysql

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

我基本上想将数据连接到我的数据库中的一行中。简单地说,如果没有现有数据,它会将文件名变量注入(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/

相关文章:

mysql - 我怎样才能加快sql查询?

php - 图片上传——BLOB 是可行的方法吗?

php - 在Elastic Search中按定义的类型排序

php - 查找大量文本数据的趋势

php - 获取一个条目的多个日期之间的平均时间

php - 登录系统无法正常工作 - 错误的字符串输出

php - 在 fopen() 失败时获取有意义的信息 (PHP/suPHP)

javascript - 无法使用 PHP 从 CollegeBoard 获取内容

java - 如何通过 Hibernate 按页查询到表?

mysql - 如何连接四个mysql表?