我有:
我想获取所有TABLE1记录并添加STATUS列:
与此同时,我从这里开始:
const getCheckboxList = (index) => {
for (let i = 0; i < attributeValues.length; i += 1) {
if (attributeValues[i].index === index) {
return attributeValues[i].value;
}
}
return [];
};
例如:
表1
SELECT t1.id,
t1.name,
t2.Address,
iif(Address is null, 'No_RECORD', 'Ok') as 'status'
FROM Table1 as t1
left join Table2 as t2 on t1.id = t2.id
表2:
id Name
111 aaa
222 bbb
333 ccc
444 ddd
555 eee
666 fff
777 ggg
888 hhh
999 iii
我接受的结果是:
id Address
111 rr
922 hfh
444 vbv
444 vbv
555 xxa
555 plo
555 plo
666 wqq
777 gyt
999 ree
999 ree
id name Address 'status'
111 aaa rr Ok
222 bbb hfh Ok
333 ccc No_RECORD
444 ddd vbv Ok
555 eee Duplicate
666 fff fff Ok
777 ggg wqq Ok
888 hhh No_RECORD
999 iii ree Ok
不是重复的,因为2 table2的记录地址是匹配的,444
是重复的,因为3个table2的记录地址不匹配,555
不是重复的,因为2 table2的记录地址是匹配的。999
没问题,因为它的id的后两位在tabl2中存在:“ 922”。我如何继续? (我在访问中使用sql查询)。
答案 0 :(得分:1)
我不知道“最后五位数字匹配”是什么意思。它与您的样本数据无关,所以我只是忽略了问题的那一部分。
您想要做的是在进行table2
之前在join
上进行汇总:
select t1.id, t1.name,
iif(multiple_addresses = 0, address, null) as address,
switch(t2.id is null, "No_Record",
multiple_addresses = 1, "Duplicate",
1=1, "OK"
) as status
from table1 as t1 left join
(select id, min(address) as address
iif(min(address) = max(address), min(address), 0, 1) as multiple_addresses
from table2
group by id
) as t2
on t1.id = t2.id;
答案 1 :(得分:0)
您可以使用case
语句获取此状态:
SELECT
t1.id,
t1.name,
t2.Address as add2,
case
when t2.Address is null and exists (select 1 from Table2 temp where temp.id = t1.id) then 'DUPLICATE'
when t2.Address is null then 'NO_RECORD'
else 'OK' end
as 'status'
FROM Table1 as t1
left join Table2 as t2 on t1.id = t2.id and t1.name = t2.Address
order by t1.id
如您所见,我在连接子句中添加了and t1.name = t2.Address
,以确保仅在需要时才具有非null的Table2值。
关于case when
,第一个条件检查是否1)没有找到对应的记录2)有相同ID的记录,即“ DUPLICATE”。第二个条件检查是否没有相应的记录,并且我们已经知道没有重复的记录(因为它会属于第一种情况)。