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 像这样的消息:
- 如果i_email和i_phone都是
nulls
我要select全部留言
- 如果 i_email 不是
null
并且 i_phone 是 null
我想 select 全部
发送到该电子邮件的消息
- 如果 i_phone 不是
null
并且 i_email 是 null
我想 select 全部
发送给 phone 的消息
- 如果i_email和i_phone都不是
nulls
我想select将所有消息发送到i_email和i_phone
我很难写成这样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 )
我有一个 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 像这样的消息:
- 如果i_email和i_phone都是
nulls
我要select全部留言 - 如果 i_email 不是
null
并且 i_phone 是null
我想 select 全部 发送到该电子邮件的消息 - 如果 i_phone 不是
null
并且 i_email 是null
我想 select 全部 发送给 phone 的消息- 如果i_email和i_phone都不是
nulls
我想select将所有消息发送到i_email和i_phone
- 如果i_email和i_phone都不是
我很难写成这样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 )