[英]Use stored procedure in select
考慮一個返回表的存儲過程:
DELIMITER //
CREATE PROCEDURE GetLegalAnimals (IN prop VARCHAR(128))
BEGIN
-- Lots of fiddling with temporary tables here
SELECT
animal, hit_points, terrain, weight, height, girth, attack, defence, quest
FROM
temp_animals;
END //
DELIMITER ;
我現在想過濾返回表的結果:
SELECT animal, terrain FROM GetLegalAnimals('rodent') WHERE hit_points<42;
上面的查詢無效,因為它不是有效的MySQL語法。 有什么方法可以執行這樣的查詢嗎? 我不想為每個(無限)條件實現一個單獨的GetLegalAnimals_*
過程,並且我也不想為該過程添加其他參數,因為存在無限的SELECT和WHERE子句排列。
如果要使用整個表,則需要一個VIEW
。 首先創建視圖
USE `yourdatabase`;
CREATE OR REPLACE VIEW `GetLegalAnimals` AS
-- Lots of fiddling with temporary tables here
SELECT
animal, hit_points, terrain, weight, height, girth, attack, defence, quest
FROM
temp_animals;
;
然后你可以查詢
SELECT animal, terrain FROM GetLegalAnimals WHERE hit_points < 42;
使用view
代替存儲過程
CREATE VIEW view_name AS
-- Lots of fiddling with temporary tables here
SELECT
animal, hit_points, terrain, weight, height, girth, attack, defence, quest
FROM
temp_animals;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.