1. 查看所有的数据表
-- 查看所有数据表; SHOW TABLES;
2. 查看某个数据表的所有的字段
-- 查看表tb_watch_back的所有字段DESCRIBE tb_watch_back;SHOW COLUMNS FROM tb_watch_back;
3. 查询数据库中某个字段以及所在的表
-- 使用 INFORMATION_SCHEMA.Columns查询所有的表以及字段SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.Columns WHERE TABLE_SCHEMA = 'super_star' AND COLUMN_NAME = 'imei';
4. 使用存储过程更新数据库中某个字段的值
-- 如果存储过程存在就删除DROP PROCEDURE IF EXISTS update_imei;CREATE PROCEDURE update_imei(IN oldImei CHAR(15),IN newImei CHAR(15))BEGIN-- 定义循环条件DECLARE flag INT DEFAULT 0; -- 保存表名DECLARE tname VARCHAR(50);-- 查询数据库super_star中含有imei列的表,如果区分大小写使用binary COLUMN_NAME = 'imei'DECLARE result CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.Columns WHERE TABLE_SCHEMA = 'super_star' AND COLUMN_NAME = 'imei';-- 退出循环DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET flag = 1; -- 打开游标OPEN result; WHILE flag <> 1 DO -- 游标指向下一个位置,可以有多个数据,比如FETCH result INTO tname,ttype,...; FETCH result INTO tname; -- 拼接字符串表名sql,根据需要使用CONCAT函数连接 -- SET @execSql = CONCAT('SELECT * FROM ',tname,' WHERE imei = ',oldImei,' ;'); SET @execSql = CONCAT('UPDATE ',tname,' SET imei = ',newImei,' WHERE imei = ',oldImei,' ;'); PREPARE stmt FROM @execSql; EXECUTE stmt; END WHILE;END;-- 调用存储过程更新数据CALL update_imei('888888888888888','864773030025354');
5. 参考文章连接
select结果赋值: http://blog.csdn.net/su12345su/article/details/52997699字符串表名转换: http://blog.csdn.net/su12345su/article/details/52997699