MySQL批处理SQL语句,mysql批处理sql
MySQL 支持批处理的模式执行一批SQL语句,下面的例子就是实验MySQL如何在windows下批处理执行SQL语句。
create table test(id int,name varchar(20));
insert into test values(1,'watson');
batchfile.txt里包含下面的一些SQL 语句,此文件放在windows系统的c:/batchmysql/batchfile.txt
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
insert into test select * from test;
在cddl数据库里进行批执行上面的语句如下:
mysql -uroot -p -D cddl < c:/batchmysql/batchfile.txt
下面是把批处理里含有查询的信息,输出保存到一个文件里:
此时的batchfile2.txt里含有query的信息(下面的3条SQL语句),下面的mysql0716.out就记录了select * from test limit 200;查询语句的结果集。
select * from test limit 200;
insert into test select * from test;
insert into test select * from test;
You can catch the output in a file for further processing:
mysql -uroot -p -D cddl < c:/batchmysql/batchfile2.txt >c:/batchmysql/mysql0716.out
@echo off
if not exist d:\rank.sql more +5 %0>d:\rank.sql
echo >nul
mysql -h localhost -u root -p 123456 < d:\rank.sql
goto :eof
truncate table a_rank;
insert into a_rank(usersn,totalpoint,point) ;
select usersn,0,Ji from ccc order by Ji desc limit 99;
update a_rank set usernick=(select usernick from b.userinfo where usersn=a_rank.usersn);
truncate table a_rank;
这个批处理可以利用mysql本身自带的mysql.exe这个程序。
@echo off
mysql -u root -p123456 -D"test" -e"show databases;"
pause
sql作业就是数据库软件本身自带的一种类似于任务计划的东西。
=.=! -e"这里面就可以写你想要运行的sql语句"
比如-e"select `name` from `xxx` where `id`=1;"
我给你解释下几个参数
-u 用户名
-p密码(-p和密码之间不要有空格,注意!)
-D"test" 连接到的数据库,也可以没有,但sql语句中要包括处理的数据库名
-e"要运行的sql语句"
你可以输入mysql -?查看mysql的参数,我记得有一个运行文本中sql语句的参数,也就是读取文件内的语句运行,但我用的不多给忘了。-?里有。