mysql, iterate through column names(mysql,遍历列名)
问题描述
我想从 MySQL 表中获取所有列名,遍历每个列名,然后使用这些列名作为变量运行存储过程.效果如下:
I would like to get all of the column names from a MySQL table, loop through each column name and then run a stored procedure using those column names as a variable. Something to the effect of:
colnames = get column names from table
for each colname
  if something changed then
    do something
  else
    do something else
看起来 SHOW COLUMNS FROM myTable 会给我列名,但我如何将列名放入循环中?
It looks like SHOW COLUMNS FROM myTable will give me the column names, but how would I get the column names into a loop?
我真的很想使用本机 SQL 在存储过程中运行所有这些.由于我仍在学习 MySQL 的复杂性,这对我的项目很有帮助.感谢您的帮助.
I would really like to run all of this in a stored procedure using native SQL. Since I'm still learning the intricacies of MySQL, and this would really help out my project. Thanks for your help.
推荐答案
我想你想要这样的:
DECLARE col_names CURSOR FOR
  SELECT column_name
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = 'tbl_name'
  ORDER BY ordinal_position;
select FOUND_ROWS() into num_rows;
SET i = 1;
the_loop: LOOP
   IF i > num_rows THEN
        CLOSE col_names;
        LEAVE the_loop;
    END IF;
    FETCH col_names 
    INTO col_name;     
     //do whatever else you need to do with the col name
    SET i = i + 1;  
END LOOP the_loop;
                        这篇关于mysql,遍历列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mysql,遍历列名
				
        
 
            
        基础教程推荐
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
 - 带更新的 sqlite CTE 2022-01-01
 - CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
 - 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
 - MySQL 5.7参照时间戳生成日期列 2022-01-01
 - 带有WHERE子句的LAG()函数 2022-01-01
 - 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
 - 从字符串 TSQL 中获取数字 2021-01-01
 - while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
 - ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				