Why I am facing a problem using the field 'from' in SQL query?


You cannot use from as a column name directly because from is a reserved word in MySQL. To avoid this, you need to use 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 some 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 a 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

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements