how pass dynamic parameter in mysql view

2019-02-17 18:11发布

I have created this in mysql

CREATE VIEW MYVIEW AS (
SELECT A.FNAME
     , A.LNAME
     , B.EMAIL
FROM EMPLOYEE A, EMPEMAIL B
WHERE A.EID = :empId
  AND A.EID = B.EID
AND B.EMAILTYP = :emailType)

now i want make " empId " and " emailType " dynamic.I mean pass the value at select time. what need to change in code?? thanx in advance

2条回答
Melony?
2楼-- · 2019-02-17 18:35

Just create the view without the parameters (i.e., to take care of the join only):

CREATE VIEW MYVIEW AS (
SELECT A.FNAME
     , A.LNAME
     , B.EMAIL
     , A.EID AS EID
     , B.EMAILTYP AS EMAILTYP
FROM EMPLOYEE A, EMPEMAIL B
WHERE A.EID = B.EID)

And apply the dynamic parameters when you query:

SELECT FNAME, LNAME, EMAIL
FROM   my_view
WHERE  eid = :empId AND emailtyp = :emailType
查看更多
霸刀☆藐视天下
3楼-- · 2019-02-17 18:45

You can use this solution with a function -

CREATE FUNCTION func() RETURNS int(11)
  RETURN @var;

CREATE VIEW view1 AS
  SELECT * FROM table1 WHERE id = func();

Using example:

SET @var = 1;
SELECT * FROM view1;
查看更多
登录 后发表回答