SQL UPDATE with LIKE

后端 未结 7 1614
夕颜
夕颜 2020-12-18 03:27

I was trying to update approx 20,000 records in a table by using statements like this one, however, I got the message say 0 row(s) affected so it didn\'t work.<

相关标签:
7条回答
  • 2020-12-18 03:48

    was looking for same but didn't work for me.... but this works perfect:

    $title = "Worklog details";
    $log_name = 'PC01';
    $update = $connection -> prepare("UPDATE nc_files 
    SET title = ? 
    WHERE log_name = ?");
    $update->bind_param('ss',$title,$log_name);
    $update->execute();
    $update->close();
    
    0 讨论(0)
  • 2020-12-18 03:52

    I had a similar trouble. The problem are the quotations marks " I Fixed my code as follow.

    UPDATE Table SET Table.Field = "myreplace" WHERE (((Table.Field) Like '%A-16%'));

    Regards, Alexwin1982

    0 讨论(0)
  • 2020-12-18 04:02
    "log_name" should not be in quotes
    
    0 讨论(0)
  • 2020-12-18 04:04

    By default MySQL allows double quoted names to be understood as either identifiers (like column names) or as string literals.

    This is meant as a convenience, but I find the semantic ambiguity frustrating. MySQL must resolve the ambiguity, and cannot magically always guess the coder's intention, as you discovered.

    -- In default sql_mode under 5.5
    --
    SELECT "foo"           -- "foo" is a *column* if tbl.foo exists, otherwise a string 
      FROM "tbl"           -- Oops!  ER_PARSE_ERROR (1064)  Can't do this in default mode.
     WHERE "foo" = "foo";  -- Both of these are strings
    

    So, the way around it is to force unambiguous interpretation of identifiers:

    1. Do not quote simple identifiers
    2. Use MySQL-specific backticks for quoting
      (This is ODBC's SQL_IDENTIFIER_QUOTE_CHAR)
    3. Always override the change the sql_mode to include ANSI_QUOTES (or a superset of it)
      Double quotes are then exclusively for identifiers, single quotes for strings.

    #3 is my personal favorite, for readability and portability. The problem is it tends to surprise people who only know MySQL, and you have to remember to override the default.

    0 讨论(0)
  • 2020-12-18 04:08

    this is because your column name log_name should not be in ' quotes.

    "log_name" LIKE "%PC01%" condition will always fail and zero rows will get updated, try this:

    UPDATE nc_files 
    SET title ="Worklog details" 
    WHERE log_name LIKE "%PC01%";
    
    0 讨论(0)
  • 2020-12-18 04:09

    The log_name is a field name, remove literal quotes from it -

    UPDATE nc_files SET title ="Worklog details" WHERE log_name LIKE "%PC01%"
    
    0 讨论(0)
提交回复
热议问题