To work with wildcards in column name for MySQL, first, you need to select column_name and then apply LIKE operator to the wildcard.
The syntax is as follows −
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ’yourTableName’ AND COLUMN_NAME LIKE 'yourWildCards%';
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table WildcardDemo -> ( -> ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ProductName varchar(10), -> Shippingdatetime datetime, -> ProductPrice int -> ); Query OK, 0 rows affected (1.31 sec)
Now you can check the description of the table. The query is as follows −
mysql> desc WildcardDemo;
Output
+------------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+-------------+------+-----+---------+----------------+ | ProductId | int(11) | NO | PRI | NULL | auto_increment | | ProductName | varchar(10) | YES | | NULL | | | Shippingdatetime | datetime | YES | | NULL | | | ProductPrice | int(11) | YES | | NULL | | +------------------+-------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec)
We have four fields and out of which three of them begin from the word Product. Now apply the wildcards in column names. The query is as follows −
mysql> select column_name from information_schema.columns -> where table_name = 'WildcardDemo' -> and column_name LIKE 'product%';
Output
+--------------+ | COLUMN_NAME | +--------------+ | ProductId | | ProductName | | ProductPrice | +--------------+ 3 rows in set (0.10 sec)