我需要像这样执行queries
:
SELECT draw,SUM(uplata) as uplata, SUM(wonamount) as isplata,
SUM(uplata)- SUM(wonamount) as stanje,
COUNT(*) as ukupno_tiketa FROM macau.tickets
WHERE shop ='105' and status != 1 and draw = 1;
我需要执行该查询100次,仅将draw = 1
更改为draw = 2
,draw = 3
等等。
我怎样才能为mysql select statement
中的每个workbench
获取一个新行,以便能够将所有内容导出到csv file
。??
答案 0 :(得分:0)
首先,您需要使用store procedure
:
while loop
drop procedure if exists load_test_data;
delimiter #
create procedure load_test_data()
begin
declare v_max int unsigned default 100;
declare v_counter int unsigned default 0;
start transaction;
truncate table new_table;
while v_counter < v_max do
insert into new_table SELECT draw,SUM(uplata) as uplata, SUM(wonamount)
as isplata, SUM(uplata)- SUM(wonamount)
as stanje, COUNT(*) as ukupno_tiketa
FROM macau.tickets WHERE
shop ='105' and status != 1 and draw = v_counter;
set v_counter=v_counter+1;
end while;
commit;
end #
delimiter ;
以后您可以调用此procedure
来运行它:
call load_test_data();
您可以将new_table
转储到csv
文件:
SELECT *
FROM new_table
INTO OUTFILE '[path]/File.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
注意:将[path]替换为您希望文件所在的文件夹。请记住,该文件夹应该是可写的,并且需要设置所有full permissions
。 Read this