0

I currently run this operation to convert empty strings to null. Is any way to find out the last row that has gone through NULLIF() operation or has null character so I can process everything from that point. My table has a timestamp column. I have 150 columns like recovery_email and I'd like to start from the last empty string was found in either of them.

UPDATE table                                                                                                 
    SET recovery_email = NULLIF(recovery_email, ''), # There are 150 columns like recovery_email.
    email = NULLIF(email, ''),
    WHERE timestamp >= (NOW() - INTERVAL 1 DAY)   

2 Answers2

0

This answers the original version of the question.

Why would you use NULLIF()? Just filter down to the rows you want:

UPDATE table                                                                                                 
    SET recovery_email = NULL                                
    WHERE timestamp >= (NOW() - INTERVAL 1 DAY) AND recovery_email = '';

You can put an index on recovery_email and not worry about attempting to filter by time.

Or, better yet, defined the column with a check constraint (in the most recent versions of MySQL) so empty strings are not allowed.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • sorry for a new update, I should have clarified that there are 150 columns like recovery email –  May 03 '20 at 21:59
0

If none of your recovery_email is null before the update query is executed, you can run the following query after the update statement to get the timestamp of the last updated row:

select max(timestamp) last_timestamp 
from mytable 
where recovery_email is null and timestamp >= now() - interval 1 day

Otherwise, you need to compute the latest value before the update, with the following query:

select max(timestamp) last_timestamp 
from mytable 
where recovery_email = '' and timestamp >= now() - interval 1 day
GMB
  • 216,147
  • 25
  • 84
  • 135
  • sorry for a new update, I should have clarified that there are 150 columns like recovery email –  May 03 '20 at 21:58