2014-10-29 71 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