【问题标题】:Update MySql Field (if field is not empty, go to next one)更新 MySql 字段(如果字段不为空,则转到下一个)
【发布时间】:2012-06-24 09:42:17
【问题描述】:

您好,我有 10 列的表(columnA、ColumnB、ColumnC ...),ColumnA 用于 ID。

我想以这种方式在 php 中进行更新:如果 ColumnB 不为空,则更新 ColumnC,如果 ColumnC 不为空,则更新 ColumnD ...直到找到一个空列。 p>

我知道如何更新一个空列,但我不知道如何实现 IF 语句。

到目前为止,我有这个:

mysql_query("UPDATE tabel_name SET ColumnB = 'example' WHERE ColumnA = '$ID' "); 

谢谢

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您必须使用case 仅更新选择性列。

    示例

    update table_name set  
        col1 = ( case when col1 is null then ? else col1 end )  
        , col2 = ( case when col2 is null then ? else col2 end )  
    --  , col3 = ...
    ;
    

    使用mysql prepare 替换查询参数。

    另请参阅Quassnoi's answer to a similar posting

    更新

    如果 ColumnB 不为空,则更新 ColumnC,如果 ColumnC 不为空,则更新 ColumnD ...直到找到一个空列

    您需要以相反的顺序设置列值检查。

    update table_name set  
    --      more next ( 4 to n ) columns here if required       
        col3 = ( case when ( col2 is not null and col3 is null ) then 7 else col3 end )  
        , col2 = ( case when ( col1 is not null and col2 is null ) then 8 else col2 end )  
        , col1 = ( case when ( col1 is null ) then 9 else col1 end )  
    ;
    

    示例表

    mysql> create table col_values ( id INT, col1 INT, col2 INT, col3 INT );
    Query OK, 0 rows affected (0.06 sec)
    
    mysql> insert into col_values values ( 1, 1, 1, NULL ), ( 2, 1, NULL, NULL ), ( 3, NULL, NULL, NULL );
    Query OK, 3 rows affected (0.01 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from col_values;
    +------+------+------+------+
    | id   | col1 | col2 | col3 |
    +------+------+------+------+
    |    1 |    1 |    1 | NULL |
    |    2 |    1 | NULL | NULL |
    |    3 | NULL | NULL | NULL |
    +------+------+------+------+
    3 rows in set (0.00 sec)
    
    mysql> update col_values set
        ->     col3 = ( case when ( col2 is not NULL and col3 is NULL ) then 7 else col3 end )
        ->     , col2 = ( case when ( col1 is not NULL and col2 is NULL ) then 8 else col2 end )
        ->     , col1 = ( case when ( col1 is NULL ) then 9 else col1 end )
        -> ;
    Query OK, 3 rows affected (0.02 sec)
    Rows matched: 3  Changed: 3  Warnings: 0
    
    mysql> select * from col_values;
    +------+------+------+------+
    | id   | col1 | col2 | col3 |
    +------+------+------+------+
    |    1 |    1 |    1 |    7 |
    |    2 |    1 |    8 | NULL |
    |    3 |    9 | NULL | NULL |
    +------+------+------+------+
    3 rows in set (0.00 sec)
    

    您也可以使用if 函数来替代case 语句。

    mysql> truncate table col_values;
    Query OK, 3 rows affected (0.01 sec)
    
    mysql> insert into col_values values ( 1, 1, 1, NULL ), ( 2, 1, NULL, NULL ), ( 3, NULL, NULL, NULL );
    Query OK, 3 rows affected (0.02 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from col_values;
    +------+------+------+------+
    | id   | col1 | col2 | col3 |
    +------+------+------+------+
    |    1 |    1 |    1 | NULL |
    |    2 |    1 | NULL | NULL |
    |    3 | NULL | NULL | NULL |
    +------+------+------+------+
    3 rows in set (0.00 sec)
    
    mysql> update col_values set
        ->     col3 = if( ( col2 is not NULL and col3 is NULL ), 7, col3 )
        ->     , col2 = if( ( col1 is not NULL and col2 is NULL ), 8, col2 )
        ->     , col1 = if( col1 is NULL, 9, col1 )
        -> ;
    Query OK, 3 rows affected (0.01 sec)
    Rows matched: 3  Changed: 3  Warnings: 0
    
    mysql> select * from col_values;
    +------+------+------+------+
    | id   | col1 | col2 | col3 |
    +------+------+------+------+
    |    1 |    1 |    1 |    7 |
    |    2 |    1 |    8 | NULL |
    |    3 |    9 | NULL | NULL |
    +------+------+------+------+
    3 rows in set (0.00 sec)
    

    【讨论】:

    • 谢谢你的回答,我需要阅读案例陈述......学习它......下面的答案正在工作
    • 这对 OP 没有帮助,因为如果 col2 & col3 为 NULL,OP 只想更新 col2 数据。 Col3 数据应为 NULL。
    • @farkasnorbert:哪个答案有效??如果它有效,请接受该答案。见here, how to accept answer
    【解决方案2】:

    你可以这样做

    UPDATE table SET columnC = IF(columnB is not null, "value", columnC) where id = 1
    

    【讨论】:

    • @farkasnorbert :意味着您将编写相同的查询来更新 columnD? UPDATE table SET columnD = IF(columnC is not null, "value", columnD) where id = 1
    【解决方案3】:

    你可以试试这样的:

        $result=mysql_query('SELECT fav1, fav2, fav3, fav4, fav5 FROM users where id=".."');
    
        $row = mysql_fetch_array($result);
    
          foreach ($row as $columnName=>$value)
          {
             if ($value==null)
                {
                 //update users set $columnName=pageID 
                 break;
                }
          }
    

    【讨论】:

    • 感谢您的回答,有些东西不起作用......让我们更具体一些......我尝试制作的是“添加到收藏夹”脚本......我有一个用户表。 ..表格看起来像这样(ID,用户名,密码,Favourite1 Favourite2 Favourite3 Favourite4 Favourite5)......在每个页面上我都有一个提交按钮......所以每个用户都可以添加5个最喜欢的页面......在他的帐户中。 ..因此,如果 Favourite1 不为空,请转到 Favourite2 并将 page_id 存储在那里...我有输入表格并且正在工作...我做了所有东西..但我不知道如何制作...存储Favourite2 如果 Favourite1 不为空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2014-03-31
    • 1970-01-01
    相关资源
    最近更新 更多