Selecting a column that is also a keyword in MySQL

余生颓废 提交于 2019-11-29 10:56:24

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.

The official term is "idiocy" :-) You can put backticks around the names such as

`ignore`

but I would give serious consideration to changing the names if possible. Backticks are not standard SQL, and I prefer my column names to be a little more expressive. For example, ignoreThisUser or orderExists (the general rule I try to follow is to have a noun and a verb in there somewhere).

Interestingly, some DBMS' can figure out not to treat it as a reserved word based on context. For example, DB2/z allows the rather hideous:

> CREATE TABLE SELECT ( SELECT VARCHAR(10) );
> INSERT INTO SELECT VALUES ('HELLO');
> SELECT SELECT FROM SELECT;
SELECT
---------+---------+---------+--------
HELLO
DSNE610I NUMBER OF ROWS DISPLAYED IS 1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!