草庐IT

mysql - 执行查询结果语句

coder 2023-10-09 原文

我有一组名为 results_% 的表,它们都具有相同的结构。

我想为这个表添加一个索引。

我可以将每个表的 alter 语句作为选择查询结果的一行,但我不知道如何执行此语句:

select concat( 'alter table ', test_db.table_name, ' add index `did` (`did`);' ) as statement 
from information_schema.tables test_db 
where test_db.table_name like 'results_%';

我错过了什么?

输出(我想执行而不是只显示给我):

+---------------------------------------------------------+
| statement                                               |
+---------------------------------------------------------+
| alter table results_Em7777_spa add index `did` (`did`); |
| alter table results_KaEng_eng add index `did` (`did`);  |
| alter table results_Ka_spa add index `did` (`did`);     |
| alter table results_Mc_spa add index `did` (`did`);     |
| alter table results_Mo_eng add index `did` (`did`);     |
| alter table results_Pe_eng add index `did` (`did`);     |
| alter table results_SU_spa add index `did` (`did`);     |
| alter table results_Ta_spa add index `did` (`did`);     |
| alter table results_ba_eng add index `did` (`did`);     |
| alter table results_br_eng add index `did` (`did`);     |
| alter table results_ca_spa add index `did` (`did`);     |
| alter table results_ch_spa add index `did` (`did`);     |
| alter table results_da_spa add index `did` (`did`);     |
| alter table results_ga_eng add index `did` (`did`);     |
| alter table results_ge_spa add index `did` (`did`);     |
| alter table results_gk_eng add index `did` (`did`);     |
+---------------------------------------------------------+
16 rows in set (0.00 sec)

[编辑]

我试过:

drop procedure if exists altlike; 
delimiter // 
create procedure altlike() 
begin 
   set group_concat_max_len = 65535; 
   select @altrlk:= concat( 'alter table ', test_db.table_name , ' add index `did` (`did`);' )
   from information_schema.tables test_db
   where test_db.table_name like "results_%"; 
   prepare statement from @altrlk; 
   execute statement; 
end // 
delimiter ; 
call altlike();

但仍然没有运气:它只改变了最后一个匹配的表 (results_gk_eng)。

最佳答案

drop procedure if exists `altlike`; 
DELIMITER // 
CREATE PROCEDURE `altlike` ()  
BEGIN
  DECLARE a,c VARCHAR(256);  
  DECLARE b INT;  
  DECLARE cur1 CURSOR FOR select concat(test_db.table_name)
  from information_schema.tables test_db 
  where test_db.table_name like 'results_%';
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET b = 1;
  DECLARE CONTINUE HANDLER FOR 1061 SET b = 0;  
  OPEN cur1;  
  SET b = 0;    
  WHILE b = 0 DO  
    FETCH cur1 INTO a;  
    IF b = 0 THEN
      SET @c = concat ('ALTER IGNORE TABLE `', a, '` ADD INDEX `did` (`did`)');
      PREPARE stmt1 FROM @c;
      EXECUTE stmt1; 
      DEALLOCATE PREPARE stmt1; 
    END IF;  
  END WHILE;  
  CLOSE cur1;       
END //  
call altlike();

关于mysql - 执行查询结果语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955732/

有关mysql - 执行查询结果语句的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  3. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  4. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  5. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  6. ruby - 如何在 Ruby 中向现有方法定义添加语句 - 2

    我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca

  7. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

  8. 报告回顾丨模型进化狂飙,DetectGPT能否识别最新模型生成结果? - 2

    导读语言模型给我们的生产生活带来了极大便利,但同时不少人也利用他们从事作弊工作。如何规避这些难辨真伪的文字所产生的负面影响也成为一大难题。在3月9日智源Live第33期活动「DetectGPT:判断文本是否为机器生成的工具」中,主讲人Eric为我们讲解了DetectGPT工作背后的思路——一种基于概率曲率检测的用于检测模型生成文本的工具,它可以帮助我们更好地分辨文章的来源和可信度,对保护信息真实、防止欺诈等方面具有重要意义。本次报告主要围绕其功能,实现和效果等展开。(文末点击“阅读原文”,查看活动回放。)Ericmitchell斯坦福大学计算机系四年级博士生,由ChelseaFinn和Chri

  9. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  10. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

随机推荐