用某个字母开头的特定类型的列,并将名称连接起来
对于以某个字母作为开头的特定类型的列,使用 LIKE。若要拼接列名,则使用 GROUP_CONCAT(),就像下面的语法一样 −
SELECT group_concat(COLUMN_NAME separator ' , ') FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = "yourTableName" AND table_schema = "yourDatabaseName" AND column_name LIKE "yourSpecificLetter%";
我们先创建一个表 −
mysql> create table DemoTable671( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(100), ClientAge int, ClientAddress varchar(200), ClientCountryName varchar(100) ); Query OK, 0 rows affected (0.62 sec)
以下是对特定类型以某个字母开头的列进行选择并获取所有列名的查询以进行拼接 −
mysql> SELECT group_concat(COLUMN_NAME separator ' , ') FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = "DemoTable671" AND table_schema = "web" AND column_name LIKE "Cl%";
这将生成以下输出 −
+-----------------------------------------------------------------------+ | group_concat(COLUMN_NAME separator ' , ') | +-----------------------------------------------------------------------+ | ClientId , ClientName , ClientAge , ClientAddress , ClientCountryName | +-----------------------------------------------------------------------+ 1 row in set (0.00 sec)
广告