简体   繁体   English

MySQL如何仅选择值包含字符串的列

[英]MySQL How to Select only columns where value contains string

I did some searching and from one question already posted on stackexchange, the answer was that it was not possible, but I figured to ask. 我做了一些搜索,并从已经在stackexchange上发布的一个问题中回答,这是不可能的,但是我想问一下。 I did not know if it was possible to form a SELECT query to dynamically select which columns will be displayed in a mysql SELECT statement result. 我不知道是否有可能形成SELECT查询来动态选择将在mysql SELECT语句结果中显示的列。 Example: 例:

Say I have column names Person, ID, Phone Number, Alt Number for this table: 假设我有此表的列名,人员,ID,电话号码,替代号码:

John | 79 | 800-499-0000 | 800-499-5555

I would like to form a SELECT statement so that it will only pull down columns where string '800-499' is somewhere in the field. 我想形成一个SELECT语句,以便它仅将下拉字段“ 800-499”在字段中某处的列。 Thus the result from MySQL ideally would be: 因此,理想情况下,MySQL的结果将是:

800-499-0000 | 800-499-5555

The only problem is that I do not think dynamically selecting columns is possible. 唯一的问题是,我认为无法动态选择列。

Any help or confirmation is appreciated. 任何帮助或确认表示赞赏。

You could try something like: 可以尝试类似:

select * from
(select concat(case when col1 like '%800-499-0000%' then concat('col1: ',col1,';') end,
               case when col2 like '%800-499-0000%' then concat('col2: ',col1,';') end,
 ...
               case when coln like '%800-499-0000%' then concat('coln: ',coln,';') end)
        as search_results
 from my_table) sq
where search_results is not null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM