mysql - 如何根据名称选择列

标签 mysql select match

我想运行一个查询来选择某些名称包含字符串的列。 假设我有要运行的列 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/

相关文章:

php - MySQL 关联表 COUNT() 和 GROUP BY

SQL 根据条件获取行

mysql - 基于选择查询更新查询

r - 在 data.table 或快速子集中快速搜索

php - PHP-api 中的 Sphinx avg 函数

MySQL View 和索引使用

html - 如何在选择下拉列表中应用占位符以及 Angular 6 中的 [ngmodel]?

php - str_ireplace() 保留大小写

struct - 你如何匹配使用rust 的特征?

php - 用于从数组获取数据的 while 循环不起作用