选择也是MySQL中的关键字的列 [英] Selecting a column that is also a keyword in MySQL
问题描述
由于某种原因,我正在工作的一家新公司的开发人员决定将其列命名为忽略"和存在".现在,当我在where子句中使用这些单词运行MySQL查询时,会出现语法错误.但是,我似乎无法弄清楚如何在不出错的情况下引用这些列.我尝试将它们设置为字符串,但这没有任何意义.
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 the where clause, I get a syntax error; however, I can't seem to figure out how to reference those columns without running into an error. I tried setting them as strings, but that doesn't make any sense.
帮助?
还有,这种不匹配的术语吗?
Also, is there a term for this kind of mismatch?
推荐答案
将名称放在反引号中:
`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
还可以.
这篇关于选择也是MySQL中的关键字的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!