SQL UPDATE with LIKE

后端 未结 7 1646
夕颜
夕颜 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 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.

提交回复
热议问题