按预查询记录中的属性查询(Oracle)
Query by attributes in pre-queried records (Oracle)
有一个tableMEETINGPARTICIPANT
如下,PK分别是ORGID
、MEETINGID
和PARTICIPANTID
Name Null? Type
------------------ -------- -------------
ORGID NOT NULL NUMBER(10)
MEETINGID NOT NULL VARCHAR2(32)
PARTICIPANTID NOT NULL VARCHAR2(32)
EMPLOYEEID NOT NULL VARCHAR2(32)
DISPLAYNAME VARCHAR2(128)
EMAIL VARCHAR2(128)
JOINTIME NOT NULL DATE
LEAVETIME NOT NULL DATE
CREATETIME NOT NULL DATE
LASTMODIFIEDTIME NOT NULL DATE
第一步,按ORGID
、MEETINGID
和EMPLOYEEID
列出记录,得到记录列表。
SELECT
*
FROM
MEETINGPARTICIPANT
WHERE
ORGID=#{orgId}
AND MEETINGID=#{meetingId}
AND EMPLOYEEID=#{employeeId}
第2步,遍历第1步的每条记录,通过第1步的EMAIL
和DISPLAYNAME
再次查找,合并成一个大记录列表
for (each record in step 1) {
SELECT
*
FROM
MEETINGPARTICIPANT
WHERE
ORGID=#{orgId}
AND MEETINGID=#{meetingId}
AND EMAIL=#{record.email} AND DISPLAYNAME=#{record.displayName}
Merge searched records to mergedRecords
}
第 3 步,returnmergedRecords
问题是,如何将第 1 步和第 2 步中的 SQL 合并为一个 SQL?
看来下面的 SQL 有效。 NVL(EMAIL, ' ')
和 NVL(DISPLAYNAME, ' ')
将处理 EMAIL 或 DISPLAYNAME 为空的记录。
SELECT
*
FROM
MEETINGPARTICIPANT
WHERE
ORGID=#{orgId}
AND MEETINGID=#{meetingId}
AND (NVL(EMAIL, ' '), NVL(DISPLAYNAME, ' ')) IN (
SELECT
NVL(EMAIL, ' '), NVL(DISPLAYNAME, ' ')
FROM
MEETINGPARTICIPANT
WHERE
ORGID=#{orgId}
AND MEETINGID=#{meetingId}
AND EMPLOYEEID=#{employeeId}
)
有一个tableMEETINGPARTICIPANT
如下,PK分别是ORGID
、MEETINGID
和PARTICIPANTID
Name Null? Type
------------------ -------- -------------
ORGID NOT NULL NUMBER(10)
MEETINGID NOT NULL VARCHAR2(32)
PARTICIPANTID NOT NULL VARCHAR2(32)
EMPLOYEEID NOT NULL VARCHAR2(32)
DISPLAYNAME VARCHAR2(128)
EMAIL VARCHAR2(128)
JOINTIME NOT NULL DATE
LEAVETIME NOT NULL DATE
CREATETIME NOT NULL DATE
LASTMODIFIEDTIME NOT NULL DATE
第一步,按ORGID
、MEETINGID
和EMPLOYEEID
列出记录,得到记录列表。
SELECT
*
FROM
MEETINGPARTICIPANT
WHERE
ORGID=#{orgId}
AND MEETINGID=#{meetingId}
AND EMPLOYEEID=#{employeeId}
第2步,遍历第1步的每条记录,通过第1步的EMAIL
和DISPLAYNAME
再次查找,合并成一个大记录列表
for (each record in step 1) {
SELECT
*
FROM
MEETINGPARTICIPANT
WHERE
ORGID=#{orgId}
AND MEETINGID=#{meetingId}
AND EMAIL=#{record.email} AND DISPLAYNAME=#{record.displayName}
Merge searched records to mergedRecords
}
第 3 步,returnmergedRecords
问题是,如何将第 1 步和第 2 步中的 SQL 合并为一个 SQL?
看来下面的 SQL 有效。 NVL(EMAIL, ' ')
和 NVL(DISPLAYNAME, ' ')
将处理 EMAIL 或 DISPLAYNAME 为空的记录。
SELECT
*
FROM
MEETINGPARTICIPANT
WHERE
ORGID=#{orgId}
AND MEETINGID=#{meetingId}
AND (NVL(EMAIL, ' '), NVL(DISPLAYNAME, ' ')) IN (
SELECT
NVL(EMAIL, ' '), NVL(DISPLAYNAME, ' ')
FROM
MEETINGPARTICIPANT
WHERE
ORGID=#{orgId}
AND MEETINGID=#{meetingId}
AND EMPLOYEEID=#{employeeId}
)