MySQL query error with a table named “order”?



The order is a reserved word. To still use a reserved word, you need to use backticks around the column name. Let us first create a table −

mysql> create table `order`    -> (    -> StudentId int    -> ); Query OK, 0 rows affected (1.78 sec)

Insert some records in the table using insert command −

mysql> insert into `order` values(101); Query OK, 1 row affected (0.26 sec) mysql> insert into `order` values(210); Query OK, 1 row affected (0.18 sec) mysql> insert into `order` values(190); Query OK, 1 row affected (0.28 sec) mysql> insert into `order` values(180); Query OK, 1 row affected (0.46 sec) mysql> insert into `order` values(205); Query OK, 1 row affected (0.28 sec)

Display all records from the table using select statement −

mysql> select *from `order`;

This will produce the following output −

+-----------+ | StudentId | +-----------+ |       101 | |       210 | |       190 | |       180 | |       205 | +-----------+ 5 rows in set (0.00 sec)

Here is the query to use ‘order’ as table name using backticks −

mysql> select *from `order` order by StudentId desc limit 1;

This will produce the following output −

+-----------+ | StudentId | +-----------+ |       210 | +-----------+ 1 row in set (0.03 sec)
Updated on: 2019-12-13T06:18:26+05:30

456 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements