本文介绍了使用一个结果集创建不同的结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过使用mysql,我得到了类似的结果
By using mysql, I got result like
count date content
19 2015-01-02 test1
6 2015-01-02 test2
3 2015-01-03 test3
12 2015-01-04 test4
48 2015-01-04 test5
10 2015-01-05 test6
我想使用日期值将该结果划分为不同的结果集.我想要结果集,
I want to divide that result in different result set using date value. I want result set like,
count date content
19 2015-01-02 test1
6 2015-01-02 test2
count date content
3 2015-01-03 test3
12 2015-01-03 test4
count date content
48 2015-01-04 test5
10 2015-01-04 test6
是否可以使用一个结果集创建不同的结果集.
Is it possible to create different result set using one result set.
推荐答案
要在查询条件中使用结果集,您需要一个游标.
To use a resultset in a query condition for a set of queries you need a cursor.
DELIMITER $$
CREATE PROCEDURE group_results_by_date
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE cdate DATE DEFAULT "2015-01-01";
-- declare cursor for getting list of dates
DEClARE date_cursor CURSOR FOR
SELECT DISTINCT (date) FROM yourtable;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
OPEN date_cursor;
get_content: LOOP
FETCH date_cursor INTO cdate;
IF v_finished = 1 THEN
LEAVE get_content;
END IF;
-- Select query for different dates
Select count, date, content from yourtable where date = cdate;
END LOOP get_content;
CLOSE date_cursor;
END$$
DELIMITER ;
您可以通过
CALL group_results_by_date();
这篇关于使用一个结果集创建不同的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!