4

こんにちは私は10列(columnA、ColumnB、ColumnC ...)のテーブルを持っています。ColumnAはID用です。

次のようにphpで更新を行います。ColumnBが空でない場合はColumnCを更新し、ColumnCが空でない場合はColumnDを更新します...空の列が見つかるまで。

空の列を更新する方法は知っていますが、IFステートメントを実装する方法がわかりません。

今まで私はこれを持っています:

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

ありがとうございました

4

3 に答える 3

4

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 の回答も参照してください。

更新

IF 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の代わりに function を使用することもできます。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)
于 2012-06-24T11:32:40.590 に答える
1

あなたは次のようなことをすることができます

UPDATE table SET columnC = IF(columnB is not null, "value", columnC) where id = 1
于 2012-06-24T10:38:39.100 に答える
0

次のようなことを試すことができます:

    $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;
            }
      }
于 2012-06-24T10:14:26.437 に答える