程序限制

时间:2013-12-01 10:47:59

标签: mysql sql stored-procedures

我需要帮助!我试着寻找答案但没有找到任何答案。

我正在使用MYSQL6。

我所做的是创建2个视图,我想限制这些视图以获得通过函数计算的特定数量的结果。

功能:

DELIMITER $$

    CREATE FUNCTION `get_total_customers`() RETURNS int(11)
BEGIN

DECLARE TotalCustomers INT;

SELECT (count(customer_id)*0.2) INTO TotalCustomers FROM customer;

RETURN TotalCustomers;
END

VIEW1:

    CREATE VIEW PreferredStatus AS 
SELECT customer_id, SUM(amount) AS TotalAmount, get_total_customers() AS Total FROM payment
GROUP BY customer_id 
ORDER BY TotalAmount DESC;

视图2:

    CREATE VIEW RegularStatus AS
SELECT customer_id, SUM(amount) AS TotalAmount, get_total_customers() AS Total FROM payment
GROUP BY customer_id 
ORDER BY TotalAmount ASC
LIMIT Total;

这是使用视图的过程。

 > DELIMITER $$

CREATE PROCEDURE `CustomerStatus`(in custID int, out status varchar(200))
BEGIN

declare number1 int;
declare totalvar int;
declare limit2 int;

IF custID in (SELECT customer_id FROM PreferredStatus) THEN 
    set status = "preferred";

ELSEIF custID in (SELECT customer_id FROM RegularStatus) THEN  
    set status = "casual";

ELSE
    set status = "regular";

End If; 
END

请帮忙!

-Thanks。

1 个答案:

答案 0 :(得分:0)

您可以将LIMIT添加到创建SELECT的{​​{1}}:

VIEW

sqlfiddle demo

来自the docs的警告:

  

如果视图定义包含LIMIT子句,则从中选择   查看使用具有自己的LIMIT子句的语句,它是未定义的   适用的限制。