我想运行一个查询来选择某些名称包含字符串的列。 假设我有要运行的列 col_1、col_2、col_3、example_1、example_2:
SELECT example_1, example_2 FROM `table`
但是有一个模式匹配..所以:
SELECT LIKE %`example_`% FROM `table`
我似乎无法在任何地方找到如何做到这一点
最佳答案
我来晚了
确实可以使用 MySQL 存储过程
DELIMITER //
create procedure custom_select()
begin
declare str_var TEXT;
select GROUP_CONCAT(column_name) into str_var from information_schema.columns where table_name ="YOUR_TABLE_NAME" and column_name like "example_%" and table_schema="YOUR_DATABASE";
set @q = CONCAT('select ', str_var, ' from table_name ');
prepare stm from @q;
execute stm;
end//
然后调用他们
delimiter ;
call custom_select();
关于mysql - 如何根据名称选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19581223/