[英]SQL - return rows that do not have a certain value
looking for a bit of help here if possible? 在可能的情况下在这里寻求帮助?
I have the following query:- 我有以下查询:
On or database we have a table called Linkfile, in this table are "Types" all beginning with "YG". 在或数据库上,我们有一个名为Linkfile的表,该表中的“类型”均以“ YG”开头。 I need to return those rows that do not have the type of "YG8" but just cannot seem to do it. 我需要返回那些不具有“ YG8”类型但似乎无法执行的行。 I know ill need to use a sub query but am stuck! 我知道我需要使用子查询,但是卡住了!
This is my code and the fields I need to return. 这是我的代码以及需要返回的字段。 I just need to only show those that do not have the lk.type of "YG8" 我只需要显示那些没有lk.type为“ YG8”的文件
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
Use below query : 使用以下查询:
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;
I've used left join on linkfile with type like 'YG8%' and fetching the only records which are not matched 我在链接文件上使用了“ YG8%”类型的左联接,并仅获取了不匹配的记录
I think you can just replace the 我认为您可以更换
lk.type like 'YG%'
with the following: 具有以下内容:
(lk.type >= 'YG' and lk.type <'YG8') or (lk.type > 'YG8' and lk.type <='YGZ')
this should accomplish what you are trying to do and also avoid using "like" which is less efficient (assuming you have an index on lk.type, at least). 这应该可以完成您要尝试执行的操作,还可以避免使用效率较低的“ like”(至少假设您在lk.type上具有索引)。
You may refine this a bit by knowing which are the possible values of lk.type of course. 您可以通过了解哪些当然是lk.type的可能值来对此进行细化。 Ie what are the extremes for the YG "subtype"? 即YG“子类型”的极端情况是什么? YG00-YG99? YG00-YG99? YG-YGZ? YG-YGZ?
(Be especially careful if you may have YG81 or YG87 for example, because then my clause will not work properly... on the other hand if your YG subtype can have values like YG34 it would have been better to use YG08 instead of YG8) (例如,如果您可能拥有YG81或YG87,请格外小心,因为这样我的子句将无法正常工作...另一方面,如果您的YG子类型可以具有类似YG34的值,则最好使用YG08而不是YG8)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.