How to find last row that ran NULLIF() or has null in mysql?

*爱你&永不变心* 提交于 2020-06-29 03:50:38

问题


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)   

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/61581845/how-to-find-last-row-that-ran-nullif-or-has-null-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!