11.mysql 查询结果存入变量:判断是否存在数据表,列名是否存在(tcy)

1.查询数据表是否存在

set @str_tb_name= (SELECT TABLE_NAME FROM information_schema.tables 
                          where table_name = 'm111' and table_schema = 'new_futures');#1条结果

set @a= (select if(@str_tb_name='m111','ok1','ng1') result);

2.查询数据列是否存在

#select (select COLUMN_NAME from  information_schema.COLUMNS 
         where table_name = 'm111'  and table_schema = 'new_futures' and  column_name='No') into @b;#多条结果

set @str_col_name= (select COLUMN_NAME from  information_schema.COLUMNS 
         where table_name = 'm111'  and table_schema = 'new_futures' and  column_name='No') ;#1条结果

set @b= (select if(@str_col_name='no','ok2','ng2') result) ;

3.显示结果:
select @a as a,@b as b,@str_tb_name as tb,@str_col_name as col;
"
a    b  tb   col
ok1 ok2 m111  No
"

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/86899007