-- 導出到指定文件
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