What makes the big difference between a backtick and an apostrophe?

前端 未结 3 1942
日久生厌
日久生厌 2020-12-19 03:20

What is the reason that the following two queries give wildly different results?

MariaDB [mydatabase]> SELECT COUNT(DISTINCT(`price`)) FROM `products`; --         


        
3条回答
  •  天涯浪人
    2020-12-19 03:29

    A straight single quote (') is used for string literals (along with straight double quote (")).

    A backtick quote (`) is for quoting identifiers.

    Identifiers must be quoted if they match a reserved word, or if they contain special characters. Quoted identifiers also can specify lowercase in case-insensitive fields (which otherwise might be shown as uppercase).

    CREATE TABLE MyTable (Field INT);
    DESCRIBE MyTable;
    +---------+-------------+------+-----+---------+-------+
    | Field   | Type        | Null | Key | Default | Extra |
    +---------+-------------+------+-----+---------+-------+
    | FIELD   | INT         | YES  |     | NULL    |       |
    +---------+-------------+------+-----+---------+-------+
    

    See also ANSI quote mode.

提交回复
热议问题