How to update column with null value

前端 未结 13 639
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 05:08

I am using mysql and need to update a column with a null value. I have tried this many different ways and the best I have gotten is an empty string.

Is there a speci

相关标签:
13条回答
  • 2020-12-13 05:50

    use is instead of =

    Eg: Select * from table_name where column is null

    0 讨论(0)
  • 2020-12-13 05:54

    If you want to set null value using update query set column value to NULL (without quotes) update tablename set columnname = NULL

    However, if you are directly editing field value inside mysql workbench then use (Esc + del) keystroke to insert null value into selected column

    0 讨论(0)
  • 2020-12-13 05:55

    if you set NULL for all records try this:

    UPDATE `table_name` SET `column_you_want_set_null`= NULL
    

    OR just set NULL for special records use WHERE

    UPDATE `table_name` SET `column_you_want_set_null`= NULL WHERE `column_name` = 'column_value' 
    
    0 讨论(0)
  • 2020-12-13 06:01

    I suspect the problem here is that quotes were entered as literals in your string value. You can set these columns to null using:

    UPDATE table SET col=NULL WHERE length(col)<3;
    

    You should of course first check that these values are indeed "" with something like:

    SELECT DISTINCT(col) FROM table WHERE length(col)<3;
    
    0 讨论(0)
  • 2020-12-13 06:06

    For those facing a similar issue, I found that when 'simulating' a SET = NULL query, PHPMyAdmin would throw an error. It's a red herring.. just run the query and all will be well.

    0 讨论(0)
  • 2020-12-13 06:07

    Another possible reason for the empty string, rather than a true null is that the field is an index or is part of an index. This happened to me: using phpMyAdmin, I edited the structure of a field in one of my tables to allow NULLs by checking the "Null" checkbox then hitting the "Save" button. "Table pricing has been altered successfully" was displayed so I assumed that the change happened -- it didn't. After doing an UPDATE to set all of those fields to NULL, they were, instead, set to empty strings, so I took a look at the table structure again and saw that the "Null" column for that field was set to 'no'. That's when I realized that the field was part of the Primary key!

    0 讨论(0)
提交回复
热议问题