2014-10-29 85 views
0

我需要这样的执行queriesMysql的选择多个ID的

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 = 1draw = 2draw = 3,等...

我该怎么办这为mysql workbench中的每个select statement获得一个新行,以便能够将所有内容导出到csv file

回答

0

首先,你需要编写一个store procedurewhile 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_tablecsv文件:

SELECT * 
FROM new_table 
INTO OUTFILE '[path]/File.csv' 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n' 

注意:将[path]替换为您希望文件在其中的文件夹。请记住,该文件夹应该是可写的,并且需要设置所有full permissionsRead this

+0

你是一个宝石伙计..谢谢你:) – BlankName 2014-10-29 22:37:46

+0

@BlankName welcome :)但如果这回答你的问题,请标记为答案,所以它被关闭。谢谢。 – Payam 2014-10-29 22:39:41