Selecting a column that is also a keyword in MySQL

后端 未结 2 700
心在旅途
心在旅途 2020-12-11 06:05

For some reason, the developers at a new company I\'m working for decided to name their columns \"ignore\" and \"exists\". Now when I run MySQL queries with those words in t

2条回答
  •  一向
    一向 (楼主)
    2020-12-11 06:30

    put the names in backticks:

    `ignore`, `exists`
    

    If you're working across multiple tables or databases you need to escape the database name, table name, and field name separately (if each matches a keyword):

    SELECT * FROM `db1`.`table1`
    LEFT JOIN `db2`.`table2` on `db1`.`table1`.`field1`=`db2`.`table2`.`field2`
    

    Only the portions that actually match a keyword have to be escaped, so things like:

    select * from `db1`.table
    

    are ok too.

提交回复
热议问题