0

Not being an SQL expert, I am having trouble with a simple replacement of a specific date-time value with NULL in MySQL. The nullable last_login DATETIME column in question may have values of '1900-01-01 00:00:00.000' as a result of populating rows from an Excel spreadsheet which do not have that date (i.e. empty cells). Each database row is uniquely identified by an id INT column. I tried:

UPDATE user_profiles
SET last_login = NULL
WHERE id IN
(SELECT id FROM user_profiles WHERE last_login = '1900-01-01 00:00:00.000');

But MySQL (and I suspect other databases as well) protests that the WHERE clause applies to the same column as the one being updated. What is the solution?

1
  • why not simply update user_profiles set last_login = NULL where last_login = '1900-01-01 00:00:00.000'? Commented Jan 28, 2016 at 22:30

3 Answers 3

2

You don't have to use a sub query in your where statement:

UPDATE user_profiles
SET last_login = NULL
WHERE last_login = '1900-01-01 00:00:00.000';
Sign up to request clarification or add additional context in comments.

1 Comment

This works fine. I was clearly over-complicating the query.
1
UPDATE CUSTOMER_ORDER
SET  PRINTED_DATE = NULL
where STATUS = 'F' and PRINTED_DATE = ''

This is in an ERP database. When the date = '', that is when reports will display 1900-01-01 etc.

Comments

0

If you want to use a sub-query with the same fields, you'll need to identify the tables. Either by using the table names or by giving them an alias. So using your code, this should work:

UPDATE user_profiles u
SET u.last_login = NULL
WHERE u.id IN
(SELECT x.id FROM user_profiles x WHERE x.last_login = '1900-01-01 00:00:00.000');

That being said, like most people are saying, you don't need to use a subquery:

UPDATE user_profiles
SET last_login = NULL
WHERE last_login = '1900-01-01 00:00:00.000';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.