-- 导出到指定文件
select * into outfile 'd:/temp/t_user.txt' from t_user;
-- 从指定文件导入数据
load data infile 'd:/temp/t_user.txt' into table t_user;
load data infile 'D:/temp/t_user.txt' into table t_status CHARACTER SET utf8;//中文乱码处理
load data infile 'd:/temp/t_user.txt' replace into table t_user;
-- 导入到指定列
load data infile 'd:/temp/t_user.txt'
into table t_user
FIELDS TERMINATED BY ' '
LINES TERMINATED BY '\n'
(id,mid,province,city);
-- 更多用法请参考 http://www.cnblogs.com/mybi/archive/2012/10/05/2711990.html
LOAD DATA [LOW_PRIORITY] [LOCAL] INFILE 'file_name.txt' [REPLACE | IGNORE]
INTO TABLE tbl_name
[FIELDS
[TERMINATED BY '\t']
[OPTIONALLY] ENCLOSED BY '']
[ESCAPED BY '\\' ]]
[LINES TERMINATED BY '\n']
[IGNORE number LINES]
[(col_name,...)]
内容替换 replace
update t_user set url = replace(url,"localhost","127.0.0.1") where id=1
缓存查看与设置
show variables like '%query_cache%'; -- 查看cache的设置
show status like '%Qcache%'; -- 性能监控
FLUSH QUERY CACHE; -- 清理查询缓存碎片以提高内存使用性能。该语句不从缓存中移出任何查询。
RESET QUERY CACHE; -- 从查询缓存中移出所有查询。FLUSH TABLES语句也执行同样的工作。
给用户赋值所有权限 grant
grant all on mytable.* to user@'10.111.1.%' identified by 'user';
flush PRIVILEGES;
存在该记录则更新,不存在则插入 DUPLICATE
insert t_user (name,age) values ('fdasf23',2) ON DUPLICATE KEY UPDATE age=age+3
若存在唯一索引冲突时,则修改 age 字段;若不存在则新增;
带分页的存储过程 存储过程实例
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`test_proc`$$
CREATE PROCEDURE `test`.`test_proc` (IN i_name VARCHAR(100),IN i_pageIndex INT, IN i_pageSize INT)
BEGIN
/*
call test_proc('a',1, 3) -- 调用示例
*/
declare stmt varchar(2000);
DROP TABLE IF EXISTS temp_tb1;
create temporary table temp_tb1(
id int(11) NOT NULL AUTO_INCREMENT,
myname varchar(50),
PRIMARY KEY (`id`)
);
insert into temp_tb1(myname)values('aa'),('abb'),('acc'),('add'),('aee');
set @sql = concat('select * from temp_tb1 where myname like ''%',i_name,'%'' limit ',(i_pageIndex-1) * i_pageSize,',',i_pageSize); -- 注意like后两个单引号表示一个。
prepare stmt from @sql;
execute stmt;
END$$
DELIMITER ;
mysql 游标 - 存储过程
BEGIN
declare i int;
-- order by 'carrier'
-- cursor 变量
DECLARE done boolean DEFAULT 0;
declare loadportname varchar(150);
DECLARE loadportnames CURSOR FOR
select t.carrier from t_abhk t where t.week_index>=week_start and t.week_index<=week_end and t.year_index=year_index
and ((port_name='all' and (destPortName='EUROPE' or destPortName='MEDITERRANEAN AND BLACK SEA')) or destPortName=port_name)
and jobType in (SELECT jobtype_name from jobtype where jobtype_id in (select jobtype_id from jobtype_group_list where jobtype_group_id=strjobtype_id))
group by t.carrier;
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
DROP TABLE IF EXISTS tem_table1;
CREATE TEMPORARY TABLE tem_table1(
week_index int,
title_value varchar(255),
total decimal(18,2)
);
DROP TABLE IF EXISTS tem_table2;
CREATE TEMPORARY TABLE tem_table2(
week_index int,
title_value varchar(255),
total decimal(18,2)
);
-- CURSOR
open loadportnames;
REPEAT
FETCH loadportnames into loadportname;
if done<>1 THEN
set i = week_start;
while i <= week_end
do
insert into tem_table1(week_index,title_value,total)values(i,loadportname,0.00);
set i = i+1;
end while;
end if;
UNTIL done END REPEAT;
close loadportnames;
insert into tem_table2(week_index,title_value,total)
select t.week_index,t.carrier,sum(noOf20_cy + noOf40_cy*2 + noOf40Hq_cy*2 + noOf45_cy*2.5 + (totalConsolCbm + totalLclCbm)/56*2) from t_abhk t
where t.week_index>=week_start and t.week_index<=week_end and t.year_index=year_index
and ((port_name='all' and (destPortName='EUROPE' or destPortName='MEDITERRANEAN AND BLACK SEA')) or destPortName=port_name)
and jobType in (SELECT jobtype_name from jobtype where jobtype_id in (select jobtype_id from jobtype_group_list where jobtype_group_id=strjobtype_id))
group by t.week_index,t.carrier;
-- update tem_table1's total
update tem_table1 tb1 INNER JOIN tem_table2 tb2 set tb1.total = tb2.total where tb1.week_index = tb2.week_index and tb1.title_value = tb2.title_value;
select * from tem_table1 order by title_value;
DROP TABLE IF EXISTS tem_table1;
DROP TABLE IF EXISTS tem_table2;
END