Can we select field name in MySQL that contains an asterisk?


No, we cannot. To still work it out, use backticks around the field name. Let us first create a table with column name with asterisk, `Name*` −

mysql> create table DemoTable
   -> (
   -> `Name*` varchar(20)
   -> );
Query OK, 0 rows affected (2.03 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(`Name*`) values('Chris Brown');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(`Name*`) values('David Miller');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable(`Name*`) values('John Doe');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable(`Name*`) values('John Smith');
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+
| Name*        |
+--------------+
| Chris Brown  |
| David Miller |
| John Doe     |
| John Smith   |
+--------------+
4 rows in set (0.00 sec)

Following is the query to select field name which contains an asterisk −

mysql> select `Name*` from DemoTable;

This will produce the following output −

+--------------+
| Name*        |
+--------------+
| Chris Brown  |
| David Miller |
| John Doe     |
| John Smith   |
+--------------+
4 rows in set (0.00 sec)

Updated on: 13-Dec-2019

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements