Select column names containing a string in MySQL?


For this, you can use SHOW COLUMNS command. Following is the syntax. Here, we have set the string using LIKE −

SHOW COLUMNS FROM yourTableName LIKE ‘yourStringValue’;

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> FirstName varchar(20),
   -> LastName varchar(20)
   -> );
Query OK, 0 rows affected (0.69 sec)

Here is the query to select column names containing a specific string −

mysql> SHOW COLUMNS FROM DemoTable LIKE 'FirstName';

Output

+-----------+-------------+------+-----+----------+-------+
| Field     | Type        | Null | Key  | Default | Extra |
+-----------+-------------+------+-----+----------+-------+
| FirstName | varchar(20) | YES  |     | NULL     |       |
+-----------+-------------+------+-----+---------+-------+
1 row in set (0.05 sec)

Updated on: 30-Jul-2019

962 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements