Manish Jangir
Manish Jangir

Reputation: 5437

Use SQL keyword as alias name of a column

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?

Upvotes: 5

Views: 3589

Answers (2)

Saharsh Shah
Saharsh Shah

Reputation: 29051

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;

Upvotes: 6

Girish
Girish

Reputation: 12127

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

Upvotes: 0

Related Questions