Mysql选择多个id

时间:2014-10-29 21:32:49

标签: mysql mysql-workbench

我需要像这样执行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 = 2draw = 3等等。

我怎样才能为mysql select statement中的每个workbench获取一个新行,以便能够将所有内容导出到csv file。??

1 个答案:

答案 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 permissionsRead this