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

MySQLMySQLi Database

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)
raja
Published on 03-May-2019 11:14:22
Advertisements