我可以在 MySQL 中创建带参数的视图吗?
我有这样的看法:
CREATE VIEW MyView AS
SELECT Column FROM Table WHERE Value = 2;
我想让它更通用,这意味着将 2 更改为变量.我试过这个:
I'd like to make it more generic, it means to change 2 into a variable. I tried this:
CREATE VIEW MyView AS
SELECT Column FROM Table WHERE Value = @MyVariable;
但 MySQL 不允许这样做.
But MySQL doesn't allow this.
我发现了一个丑陋的解决方法:
I found an ugly workaround:
CREATE FUNCTION GetMyVariable() RETURNS INTEGER DETERMINISTIC NO SQL
BEGIN RETURN @MyVariable; END|
然后视图是:
CREATE VIEW MyView AS
SELECT Column FROM Table WHERE Value = GetMyVariable();
但它看起来真的很糟糕,而且用法也很糟糕——我必须在每次使用视图之前设置@MyVariable.
But it looks really crappy, and the usage is also crappy - I have to set the @MyVariable before each usage of the view.
有没有我可以这样使用的解决方案:
Is there a solution, that I could use like this:
SELECT Column FROM MyView(2) WHERE (...)
具体情况如下:我有一个表存储有关被拒绝的请求的信息:
The concrete situation is as follows: I have a table storing information about the denied request:
CREATE TABLE Denial
(
Id INTEGER UNSIGNED AUTO_INCREMENT,
PRIMARY KEY(Id),
DateTime DATETIME NOT NULL,
FeatureId MEDIUMINT UNSIGNED NOT NULL,
FOREIGN KEY (FeatureId)
REFERENCES Feature (Id)
ON UPDATE CASCADE ON DELETE RESTRICT,
UserHostId MEDIUMINT UNSIGNED NOT NULL,
FOREIGN KEY (UserHostId)
REFERENCES UserHost (Id)
ON UPDATE CASCADE ON DELETE RESTRICT,
Multiplicity MEDIUMINT UNSIGNED NOT NULL DEFAULT 1,
UNIQUE INDEX DenialIndex (FeatureId, DateTime, UserHostId)
) ENGINE = InnoDB;
多重性是在同一秒内记录的相同请求的数量.我想显示拒绝列表,但有时,当应用程序被拒绝时,它会重试几次以确保.因此,通常情况下,当同一用户在几秒钟内在同一功能上被拒绝 3 次时,实际上是一次拒绝.如果我们有更多的资源来满足这个请求,接下来的两次拒绝就不会发生.因此,我们希望在报告中对拒绝进行分组,允许用户指定拒绝分组的时间跨度.例如.如果我们在时间戳中拒绝(对于功能 1 上的用户 1):1,2,24,26,27,45 并且用户想要将彼此距离更近的拒绝分组超过 4 秒,他应该得到这样的结果:1(x2)、24 (x3)、45 (x1).我们可以假设,真实否认之间的空间比重复之间的空间大得多.我通过以下方式解决了这个问题:
A multiplicity is a number of identical requests recorded in the same second. I want to display a list of denials, but sometimes, when the application gets denied, it retries a couple times just to make sure. So usually, when the same user gets denial 3 times on the same feature in a couple seconds it is actually one denial. If we'd have one more resource, to fulfill this request, the next two denials would not happen. So we want to group the denials in report allowing the user to specify the timespan in which denials should be grouped. E.g. if we have denials (for user 1 on feature 1) in timestamps: 1,2,24,26,27,45 and user wants to group denials that are closer to each other than 4 sec, he should get something like this: 1 (x2), 24 (x3), 45 (x1). We can assume, that spaces between real denials are much bigger than between duplications. I solved the problem in the following way:
CREATE FUNCTION GetDenialMergingTime()
RETURNS INTEGER UNSIGNED
DETERMINISTIC NO SQL
BEGIN
IF ISNULL(@DenialMergingTime) THEN
RETURN 0;
ELSE
RETURN @DenialMergingTime;
END IF;
END|
CREATE VIEW MergedDenialsViewHelper AS
SELECT MIN(Second.DateTime) AS GroupTime,
First.FeatureId,
First.UserHostId,
SUM(Second.Multiplicity) AS MultiplicitySum
FROM Denial AS First
JOIN Denial AS Second
ON First.FeatureId = Second.FeatureId
AND First.UserHostId = Second.UserHostId
AND First.DateTime >= Second.DateTime
AND First.DateTime - Second.DateTime < GetDenialMergingTime()
GROUP BY First.DateTime, First.FeatureId, First.UserHostId, First.Licenses;
CREATE VIEW MergedDenials AS
SELECT GroupTime,
FeatureId,
UserHostId,
MAX(MultiplicitySum) AS MultiplicitySum
FROM MergedDenialsViewHelper
GROUP BY GroupTime, FeatureId, UserHostId;
然后要显示用户 1 和 2 对每 5 秒合并一次的功能 3 和 4 的拒绝,您所要做的就是:
Then to show denials from user 1 and 2 on features 3 and 4 merged every 5 seconds all you have to do is:
SET @DenialMergingTime := 5;
SELECT GroupTime, FeatureId, UserHostId, MultiplicitySum FROM MergedDenials WHERE UserHostId IN (1, 2) AND FeatureId IN (3, 4);
我使用视图是因为它很容易过滤数据并在 jQuery 网格中显式使用它、自动排序、限制记录数等.
I use the view because in it it's easy to filter data and to use it explicitly in the jQuery grid, automatically order, limit number of records and so on.
但这只是一个丑陋的解决方法.有没有合适的方法来做到这一点?
But it's just an ugly workaround. Is there a proper way to do this?
推荐答案
其实如果你创建func:
Actually if you create func:
create function p1() returns INTEGER DETERMINISTIC NO SQL return @p1;
并查看:
create view h_parm as
select * from sw_hardware_big where unit_id = p1() ;
然后你可以调用一个带有参数的视图:
Then you can call a view with a parameter:
select s.* from (select @p1:=12 p) parm , h_parm s;
希望能帮到你.
相关文章