要在MySQL的列名中使用通配符,首先,需要选择column_name,然后将LIKE运算符应用于通配符。
语法如下-
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ’yourTableName’ AND COLUMN_NAME LIKE 'yourWildCards%';
为了理解上述语法,让我们创建一个表。创建表的查询如下-
create table WildcardDemo -> ( -> ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ProductName varchar(10), -> Shippingdatetime datetime, -> ProductPrice int -> );
现在,您可以检查表的描述。查询如下-
desc WildcardDemo;
输出结果
+------------------+-------------+------+-----+---------+----------------+ | 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)
我们有四个字段,其中三个字段以“产品”一词开头。现在,在列名称中应用通配符。查询如下-
select column_name from information_schema.columns -> where table_name = 'WildcardDemo' -> and column_name LIKE 'product%';
输出结果
+--------------+ | COLUMN_NAME | +--------------+ | ProductId | | ProductName | | ProductPrice | +--------------+ 3 rows in set (0.10 sec)