0

Is there a way to escape the keywords in mySQL. For instance, I want to add an alias name as 'AS'. mySQL is not allowing me to do that because AS is already a keyword.

In the below example, AS is the alias name for the table. SELECT * FROM Astrology AS where AS.column_name is not null;

Note: I remember we use something like [AS] to treat them as normal text in MS-SQL. I would like to know how we could do this in mySql.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Hari
  • 19
  • 2
  • have you tried square brackets [AS] ? – Amit Verma Feb 02 '22 at 09:14
  • 4
    @AmitVerma Only T-SQL (MS SQL Server and Sybase) uses `[]` for escapes. ISO SQL (which all RDMBS support, _aka_ ANSI SQL) uses double-quotes and should be the preferred way to escape names. As for MySQL, it uses backticks for escapes. – Dai Feb 02 '22 at 09:18
  • 1
    Or simply avoid choosing identifiers that are reserved words. https://en.wikipedia.org/wiki/SQL_reserved_words – jarlh Feb 02 '22 at 09:21
  • https://dev.mysql.com/doc/refman/8.0/en/identifiers.html contains the answer. – Akina Feb 02 '22 at 09:37
  • Thanks, backticks does the trick. Eg: SELECT * FROM `select` WHERE `select`.id > 100; – Hari Feb 02 '22 at 14:50

0 Answers0