CREATE TABLE registration
(
Id INT NOT NULL AUTO_INCREMENT,
CONT_ID NUMBER(10,0);
COMM_ID NUMBER(10,0);(Can have null values Nullable)
PRIMARY KEY(Id)
);
CREATE TABLE Contact
(
Id INT NOT NULL AUTO_INCREMENT,
FirstName CHAR(30 BYTE)
LastName CHAR(30 BYTE)
);
CREATE TABLE Communication
(
Id INT NOT NULL AUTO_INCREMENT,
FirstName CHAR(30 BYTE)
LastName CHAR(30 BYTE)
);
我有一张登记表,上面有
RegistratonId
、Contact_Id
、CommunicationId
、Remark
。
还有一张
Contact
桌子
ContactID FirstName LastName
还有一张
Communication
桌子
CommunicationId FirstName LastName
我需要导出一个条件,例如对于特定的注册 ID,如果联系人表的名字和姓氏与通信表的名字和姓氏相同,则将变量设置为匹配。如果comm id为null则可以通过ismatching设置为true
无论您使用什么数据库(并且未指定),它看起来都是(外部)连接以及
case
表达式。
样本数据:
SQL> with
2 registration (id, cont_id, comm_id) as
3 (select 1, 10, 20 from dual union all
4 select 2, 11, 21 from dual union all
5 select 3, 12, null from dual
6 ),
7 contact (id, firstname, lastname) as
8 (select 10, 'Little', 'Foot' from dual union all
9 select 11, 'Scott', 'Tiger' from dual union all
10 select 12, 'Michael', 'Jackson' from dual
11 ),
12 communication (id, firstname, lastname) as
13 (select 20, 'Little', 'Foot' from dual union all
14 select 21, 'Pixie', 'Dixie' from dual
15 )
16 select
查询:
17 r.id reg_id,
18 t.firstname ||' '|| t.lastname contact_name,
19 m.firstname ||' '|| m.lastname comm_name,
20 --
21 case when t.firstname ||' '|| t.lastname = m.firstname ||' '|| m.lastname then 'yes'
22 else 'no'
23 end as ismatching
24 from registration r join contact t on t.id = r.cont_id
25 left join communication m on m.id = r.comm_id;
REG_ID CONTACT_NAME COMM_NAME ISMATCHING
---------- --------------- ------------ ----------
1 Little Foot Little Foot yes
2 Scott Tiger Pixie Dixie no
3 Michael Jackson no
SQL>
在 Oracle 中,它的 SQL 不支持版本 23c 之前的布尔数据类型(我没有;你有吗?)所以(因为这个示例是在 Oracle 11g 中运行的)我选择为 ismatching
返回一个 string
专栏。