MySQL 列名中是否包含通配符?
mysqlmysqli database
要使用 MySQL 列名中的通配符,首先,您需要选择 column_name,然后将 LIKE 运算符应用于通配符。
语法如下 −
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ’yourTableName’ AND COLUMN_NAME LIKE 'yourWildCards%';
为了理解上述语法,让我们创建一个表。创建表的查询如下 −
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)
现在可以查看表的描述。查询如下 −
mysql> 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)
我们有四个字段,其中三个以单词 Product 开头。现在在列名中应用通配符。查询如下 −
mysql> 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)