Hello I have table with 10 columns (columnA, ColumnB, ColumnC ...), ColumnA is for ID.
I want to make an update in php in this way: IF ColumnB is not empty then update ColumnC, if ColumnC is not empty update ColumnD ...until it finds an empty column.
I know how to update an empty column but i can`t figurit out how to implement the IF statement.
Until now i have this:
mysql_query("UPDATE tabel_name SET ColumnB = 'example' WHERE ColumnA = '$ID' ");
Thank You
解决方案
You have to use case to update only selective columns.
Example:
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 = ...
;
Use mysql prepare to replace query parameters.
Update: