需要在一个表中返回结果的Mysql程序

时间:2017-01-27 10:20:19

标签: mysql

我想在一个表中执行以下过程输出。实际上,当我将它运行到HeidlSql中时,它会在多个表中显示输出。

DELIMITER //
create procedure daily_report()
BEGIN
select count(Transaction1) from test1 where (DATE1 between curdate() and curdate()+1);
select count(Transaction1) from test1 where RESPONSE='Y' and (DATE1 between curdate() and curdate()+1);
select (sum(RESPONSE='Y')/count(Transaction1))*100 from test1 where (DATE1 between curdate() and curdate()+1);
select count(Transaction1) from test1 where RESPONSE='N' and (DATE1 between curdate() and curdate()+1);
select (sum(RESPONSE='N')/count(Transaction1))*100 from test1 where (DATE1 between curdate() and curdate()+1);
select count(Transaction1),DATE1 from test1 where RESPONSE='N' and ERROR1='30' and (DATE1 between curdate() and curdate()+1);
select (sum(RESPONSE='N' AND ERROR1='30')/count(Transaction1))*100 from test1 where (DATE1 between curdate() and curdate()+1);
select count(Transaction1) from test1 where RESPONSE='N' and ERROR1 not like '30' and (DATE1 between curdate() and curdate()+1);
select (sum(RESPONSE='N' AND ERROR1 not like '30')/count(Transaction1))*100 from test1 where (DATE1 between curdate() and curdate()+1);
end
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

如果您想获得单个结果集,则可以将查询与UNION结合使用 需要注意的是,它们都必须返回相同数量的列。

SELECT 'metric1' AS name, count(Transaction1) AS value1, NULL AS value2 from test1 where (DATE1 between curdate() and curdate()+1)
UNION
SELECT 'metric2', count(Transaction1), NULL from test1 where RESPONSE='Y' and (DATE1 between curdate() and curdate()+1)
UNION
select 'metric3', (sum(RESPONSE='Y')/count(Transaction1))*100, NULL from test1 where (DATE1 between curdate() and curdate()+1)
UNION
select 'metric4', count(Transaction1),DATE1 from test1 where RESPONSE='N' and ERROR1='30' and (DATE1 between curdate() and curdate()+1);