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: 2019-07-30T22:30:26+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements