Use SQL keyword as alias name of a column

此生再无相见时 提交于 2019-12-12 09:05:09

问题


I'm executing the following query but it is giving syntax error. Because the keyword key is registered in SQL

SELECT `id` AS key, `country_name` AS value FROM countries

I also tried using brackets like this but it's not working:

SELECT `id` AS [key], `country_name` AS value FROM countries

How to deal with it?


回答1:


Use backtick(`) or Single Quote(') to give alias name of column in MySQL.

Try this:

SELECT `id` AS 'key', `country_name` AS value 
FROM countries;

OR

SELECT `id` AS `key`, `country_name` AS value 
FROM countries;



回答2:


key is mysql keyword so compiler suppose as mysql keyword, you should warp by apostrophe(`) to column name

SELECT `id` AS `key`, `country_name` AS value 
FROM countries


来源:https://stackoverflow.com/questions/27309977/use-sql-keyword-as-alias-name-of-a-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!