gpt4 book ai didi

Mysql 选择多个 id

转载 作者:行者123 更新时间:2023-11-29 23:30:18 25 4
gpt4 key购买 nike

我需要像这样执行查询:

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 workbench 中的每个 select 语句获取一个新行,以便能够将所有内容导出到 csv 文件 .??

最佳答案

首先,您需要编写一个带有 while 循环存储过程:

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 ;

稍后您可以调用此过程来运行它:

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] 替换为您希望文件所在的文件夹。请记住,该文件夹应该是可写的,并且需要设置所有完全权限Read this

关于Mysql 选择多个 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26641043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com