Problem using the column name 'from' in a MySQL query?


You cannot use ‘from’ as column name directly because ‘from’ is a reserved word in MySQL.

If you want to still use it, then you need to use the backtick symbol.

Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   `from` varchar(100),
   Name varchar(10)
   );
Query OK, 0 rows affected (0.92 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable(`from`,Name) values('US','John');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(`from`,Name) values('UK','Carol');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(`from`,Name) values('AUS','David');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+----+------+-------+
| Id | from | Name  |
+----+------+-------+
| 1  | US   | John  |
| 2  | UK   | Carol |
| 3  | AUS  | David |
+----+------+-------+
3 rows in set (0.00 sec)

Following is the query to use ‘from’ reserved word as column name −

mysql> select `from` from DemoTable;

This will produce the following output −

+------+
| from |
+------+
| US   |
| UK   |
| AUS  |
+------+
3 rows in set (0.00 sec)

Updated on: 30-Jul-2019

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements