8

I have created the following view 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)

I have to make empId and emailType dynamic i.e. get the result for the desired values of empId and emailType at runtime.

What changes should be made in the code?

Thanks in advance.

4

2 回答 2

15

您可以将此解决方案与功能一起使用 -

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

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

使用示例:

SET @var = 1;
SELECT * FROM view1;
于 2013-11-13T07:50:14.877 回答
14

只需创建不带参数的视图(即,仅处理连接):

CREATE VIEW MYVIEW AS (
SELECT A.FNAME
     , A.LNAME
     , B.EMAIL
     , A.EID AS EID             -- added to be used in the WHERE
     , B.EMAILTYP AS EMAILTYP   -- added to be used in the WHERE
FROM EMPLOYEE A, EMPEMAIL B
WHERE A.EID = B.EID)

并在查询时应用动态参数:

SELECT FNAME, LNAME, EMAIL
FROM   my_view
WHERE  eid = 'your_empId' AND emailtyp = 'your_emailType'

注意WHERE上面显示的,它使用了在VIEW

于 2013-11-13T06:10:10.620 回答