Select column names containing a string in MySQL?

MySQLMySQLi Database

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)
raja
Published on 09-Jul-2019 11:37:10
Advertisements