我需要一个存储过程的示例,该存储过程使用另外两个存储过程(甚至只是一个)的结果,其中结果用作组合存储过程中的表。我很可能从那里拿走它。
网站上的示例(多个网站)和php.net上的详细信息过于简单。
基本问题可以在这里看到:
SELECT ttd.person_id, weight, min(test_date)
FROM (
CALL get_team_member_body_stats(1) AS tbs,
CALL get_team_member_first_last_test_date(1) AS ttd
WHERE tbs.person_id = ttd.person_id
AND (tbs.test_date = ttd.first_test OR tbs.test_date = ttd.last_test)
GROUP BY ttd.person_id;
END
感谢您的帮助。昨天和今天,我在网上搜索和试验中已经打了几个小时。
详细信息:
存储过程get_team_member_body_stats(IN team_id INT)适用于5个表(person,team,person_team,body_stats,person_body_stats)并生成:
person_id | body_stats_id | weight | test_date |
2 2 200 2011-01-01
4 3 250 2011-01-01
1 5 145 2011-03-01
2 6 210.4 2011-03-01
5 7 290 2011-03-01
1 8 140 2011-04-01
1 9 135 2011-05-01
4 11 245 2011-05-01
存储过程get_team_member_first_last_test_date(IN team_id INT)在相同的表上工作并产生:
person_id | first_test | last_test
1 2011-03-01 2011-05-01
2 2011-01-01 2011-03-01
4 2011-01-01 2011-05-01
5 2011-03-01 2011-03-01
目标是加入这些并产生:
person_id | first_weight | last_weight
1 145 135
2 200 210.4
4 250 245
5 290 290
由于
答案 0 :(得分:1)
看来(根据这个社区,因为我找不到任何使用其他存储过程的结果集作为新选择表中的表的存储过程的例子),mysql要么不支持这个,要么是非常难以做我希望做的事。
因此,我没有调用存储过程,而是将查询复制到最终的过程中。
DELIMITER $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `get_team_stats_change`( IN team_id INT ) BEGIN SELECT SUM(start_weight) AS start_weight, SUM(start_body_fat) AS start_body_fat, SUM(current_weight) AS current_weight, SUM(current_body_fat) AS current_body_fat, SUM(weight_change) AS weight_change, SUM(body_fat_change) AS body_fat_change FROM ( SELECT ls.person_id, fs.weight AS start_weight, fs.body_fat AS start_body_fat, ls.weight AS current_weight, ls.body_fat AS current_body_fat, fs.weight - ls.weight AS weight_change, fs.body_fat - ls.body_fat AS body_fat_change FROM (SELECT ttd.person_id, bs.weight, bs.body_fat, bs.test_date FROM body_stats AS bs JOIN ((SELECT pbs.person_id, min(bs.test_date) AS first_test, max(bs.test_date) AS last_test FROM body_stats AS bs, person_body_stats AS pbs, team_member AS tm, team AS t WHERE t.team_id = team_id AND tm.team_id = t.team_id AND tm.person_id = pbs.person_id AND pbs.body_stats_id = bs.body_stats_id AND tm.start_date bs.test_date AND bs.test_date >= t.starting_date GROUP BY person_id) AS ttd, person_body_stats AS pbs) ON bs.test_date = ttd.first_test AND pbs.person_id = ttd.person_id AND pbs.body_stats_id = bs.body_stats_id) AS fs, (SELECT ttd.person_id, bs.weight, bs.body_fat, bs.test_date FROM body_stats AS bs JOIN ((SELECT pbs.person_id, min(bs.test_date) AS first_test, max(bs.test_date) AS last_test FROM body_stats AS bs, person_body_stats AS pbs, team_member AS tm, team AS t WHERE t.team_id = team_id AND tm.team_id = t.team_id AND tm.person_id = pbs.person_id AND pbs.body_stats_id = bs.body_stats_id AND tm.start_date bs.test_date AND bs.test_date >= t.starting_date GROUP BY person_id) AS ttd, person_body_stats AS pbs) ON bs.test_date = ttd.last_test AND pbs.person_id = ttd.person_id AND pbs.body_stats_id = bs.body_stats_id) AS ls WHERE ls.person_id = fs.person_id ) AS delta; END
答案 1 :(得分:0)
我不知道如何轻松地从5个表合并到临时表或视图中获得这些结果,但实质上,以下内容应该与您当前尝试使用3个存储过程的内容相同。 / p>
SELECT person_id
, (SELECT weight
FROM FiveTablesResult
WHERE person_id = ftr.person_id
AND test_date = (SELECT MIN(test_date)
FROM FiveTablesResult
WHERE person_id = ftr.person_id)
) AS first_weight
, (SELECT weight
FROM FiveTablesResult
WHERE person_id = ftr.person_id
AND test_date = (SELECT MAX(test_date)
FROM FiveTablesResult
WHERE person_id = ftr.person_id)
) AS last_weight
FROM FiveTablesResult ftr
请注意,还有优化空间,但让我们先回复正确的结果