我有以下查询: -
在或数据库中我们有一个名为Linkfile的表,在这个表中是“类型”,都以“YG”开头。我需要返回那些没有“YG8”类型的行,但似乎无法做到。我知道不需要使用子查询但是卡住了!
这是我的代码和我需要返回的字段。我只需要展示那些没有“YG8”的lk.type
的人select distinct l.description, p.displayname AS Temp, p.compliance_status As 'Compliant', lk.displayname, lk.type
from event e
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on p.person_ref = pt.person_ref
inner join linkfile lk on lk.parent_object_ref = pt.person_ref
where o.displayname LIKE '%G4S%' and p.compliance_category = '$016'
and lk.type like 'YG%' and l.code_type = '2'
and a.displayname LIKE '%MOJ%'
and pt.status = 'A'
order by l.description, p.displayname, lk.type
答案 0 :(得分:0)
使用以下查询:
select distinct l.description, p.displayname AS Temp, p.compliance_status As 'Compliant', lk.displayname, lk.type,lk.parent_object_ref
from event e
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on p.person_ref = pt.person_ref
left join (select displayname, type,parent_object_ref from linkfile where lk.type like 'YG8%' )lk on lk.parent_object_ref = pt.person_ref
where o.displayname LIKE '%G4S%' and p.compliance_category = '$016' and lk.parent_object_ref is null
and l.code_type = '2'
and a.displayname LIKE '%MOJ%'
and pt.status = 'A'
order by l.description, p.displayname, lk.type;
我在链接文件上使用了左连接,类型为' YG8%'并获取唯一不匹配的记录
答案 1 :(得分:0)
我认为你可以直接替换
lk.type like 'YG%'
以下内容:
(lk.type >= 'YG' and lk.type <'YG8') or (lk.type > 'YG8' and lk.type <='YGZ')
这应该完成你想要做的事情,也避免使用&#34;喜欢&#34;哪个效率较低(假设你至少有lk.type的索引)。
您可以通过了解当然lk.type的可能值来改进这一点。即YG&#34;子类型&#34;的极端情况是什么? YG00,YG99? YG-YGZ?
(如果您可能有YG81或YG87,请特别小心,因为那时我的条款将无法正常工作......另一方面,如果您的YG子类型可以具有YG34之类的值,那么最好使用YG08代替YG8)