MySQL:选择包含字符串的列名 [英] MySQL: Select Column names containing a string
问题描述
我正在尝试从名称中包含特定字符串的列中获取数据,对于我来说,它是 PLA .尽管它们是一种选择值的方式,如 MySQL查询字符串包含.列名有类似的方法吗?
I am trying to get data from columns containing a particular string in their name in my case it it PLA. Though their is a way to select where the values are like as in MySQL query String contains. Is there a similar way for the column name?
SELECT COLUMN NAMES LIKE '%PLA%' FROM `TABLENAME` WHERE MONTH=? AND YEAR=?
推荐答案
SELECT COLUMN NAMES
在MySQL中无效.有类似的内容,例如DESCRIBE [table];
或SHOW COLUMNS FROM [table];
,但是您不能在其上放置WHERE子句.更好的方法是使用INFORMATION_SCHEMA
虚拟数据库,例如-
SELECT COLUMN NAMES
is not valid in MySQL. There are similar things such as DESCRIBE [table];
or SHOW COLUMNS FROM [table];
, but you can't put WHERE clauses on them. Much better is to use the INFORMATION_SCHEMA
virtual DB e.g. -
SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='[database name]' and `TABLE_NAME`='[table name]'
AND `COLUMN_NAME` LIKE '%PLA%'
请参见 http://dev.mysql.com/doc/refman/5.6/en/columns-table.html 了解更多信息.
这篇关于MySQL:选择包含字符串的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!