How to select a column name with spaces in MySQL?

MySQLMySQLi Database

To select a column name with spaces, use the back tick symbol with column name. The symbol is ( ` `). Back tick is displayed in the keyboard below the tilde operator ( ~).

Tidle Operator

Firstly, create a table −

mysql> CREATE table SpaceColumn
-> (
-> `Student Name` varchar(100)
-> );
Query OK, 0 rows affected (0.48 sec)

Inserting records

mysql> INSERT into SpaceColumn values('John');
Query OK, 1 row affected (0.18 sec)

mysql> INSERT into SpaceColumn values('Bob');
Query OK, 1 row affected (0.17 sec)

The syntax to get column name with space is as follows −

SELECT `column_name` from yourTableName;

Now I will apply the above syntax to get the result for my column. The query is as follows −

mysql> SELECT `Student Name` from SpaceColumn;

The following is the output −

+--------------+
| Student Name |
+--------------+
| John         |
| Bob          |
+--------------+
2 rows in set (0.00 sec)
raja
Published on 18-Oct-2018 12:59:02
Advertisements