问题描述
我有以下两个表TestCustomer和Testemail。我想基于TestCustomer上的email_stat和外部布尔参数“ noFlag”检索emailAddress,email_stat。如果noFlag为“ true”,则从 Testemail 表和与customer_id关联的email_stat TestCustomer ,否则只能获得“ A”。任何帮助。
create table Testemail(emai_id varchar(18),emailaddress varchar(20))
create table TestCustomer(customer_id varchar(18),emai_id varchar(18),email_stat char(1))
Insert Into Testemail(emai_id,emailaddress)values('12345','[email protected]');
Insert Into Testemail(emai_id,emailaddress)values('123456','[email protected]');
Insert Into Testemail(emai_id,emailaddress)values('123457','[email protected]');
Insert Into TestCustomer(customer_id,emai_id,email_stat)values('223459','12345','A');
Insert Into TestCustomer(customer_id,email_stat)values('223458','123456','I');
Insert Into TestCustomer(customer_id,'123457','A');
预期输入:
customer_id = 223458和noFlag ='true'
预期产量
预期输入:
customer_id = 223458和noFlag ='false'
预期产量
空结果。
解决方法
您要的是pl / sql代码吗? 因为我不熟悉在没有pl \ sql功能的情况下在sql查询中获取外部参数的方法...
,您可以创建一个虚拟列以将其标记为“ true”或“ false”:
1. Location string `gorm:"where:location > 0" json:"location"`
2. Location string `gorm:"where:location <> ''" json:"location"`
3. Location string `gorm:"check: location > 0" json:"location"`
4. Location string `gorm:"check: location <> '' " json:"location"`
5. Location string `gorm:"check: location <> "" " json:"location"`
查询1:
create table TestCustomer(customer_id varchar(18),emai_id varchar(18),email_stat char(1),noflag as (decode(email_stat,'I','true','false')));
查询2:
select tc.customer_id,email_stat,te.emailaddress from TestCustomer tc join TestEmail te on tc.emai_id = te.emai_id where tc.customer_id=223458 and tc.noflag='true';
演示: https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=6291bf905c3387ff07e0ff145013afee
替代解决方案:
- 表结构不变
- 使用noflag列创建视图
数据库视图:
select tc.customer_id,te.emailaddress from TestCustomer tc join TestEmail te on tc.emai_id = te.emai_id where tc.customer_id=223458 and tc.noflag='false';
查询:
CREATE OR REPLACE VIEW vw_email AS
SELECT tc.customer_id,tc.email_stat,te.emailaddress,decode(tc.email_stat,'false') as noflag
FROM TestCustomer tc
JOIN TestEmail te on tc.emai_id = te.emai_id;
演示:
SELECT customer_id,emailaddress FROM vw_email WHERE customer_id=223458 AND noflag='true';