Oracle select - 多个参数一列

Oracle select - multiple parameters one column

我有一个 table 列如下:

ID number
Adress varchar2
Message varchar2

地址可以有邮箱或者phone

我有一个这样的存储过程:

PROCEDURE get_messages (
  i_email                   IN     varchar2,
  i_phone                   IN     varchar2
  o_messages    OUT     messages)
BEGIN

收到电子邮件,phone 或两者。

在此过程中,我想 select 像这样的消息:

我很难写成这样select

  SELECT * FROM MESSAGES
  WHERE (Address = i_email OR i_email IS NULL)
         OR
        (Address = i_phone OR i_phone IS NULL)

但只有当两个值都不是 nulls

时它才有效

试试这个。

SELECT *
FROM   MESSAGES
WHERE  ( i_email IS NULL
         AND i_phone IS NULL )
        OR ( i_email IS NOT NULL
             AND i_phone IS NULL
             AND Address = i_email )
        OR ( i_phone IS NOT NULL
             AND i_email IS NULL
             AND Address = i_phone )
        OR ( Address = i_email
             OR i_phone = Address )