简体   繁体   中英

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. 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. Example:

Say I have column names Person, ID, Phone Number, Alt Number for this table:

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. Thus the result from MySQL ideally would be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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