简体   繁体   English

SQL-返回没有特定值的行

[英]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.

相关问题 SQL查询仅返回特定列中具有最高值的行 - SQL query to only return the rows that have the highest value in a certain column 返回某个字段中没有值的记录 - Return records that do not have a value in a certain field 使用 SQL,如何计算具有特定值的行的百分比? - With SQL, how do I calculate what percentage of rows have a certain value? sql 只返回具有特定列值的行 - sql only return rows that have certain column values SQL查询仅根据列的值返回某些行 - SQL Query to only return certain rows depending on value of column 在 SQL 中返回每个组具有最大值的行,包括具有相同值的行 - In SQL return rows with max value for each group, including rows that have the same value SQL查询返回的列的值大于某个最小值(取决于类别)的行? - SQL query to return rows with the value of a column above a certain minimum value depending on category? SQL:选择在特定列中具有特定值的帐户的所有行 - SQL: Select all rows of the accounts which have certain value in a particular column 如何在asp.net中查找SQL数据库中具有某个布尔列值为true的所有行? - How to lookup all rows in an SQL database that have a certain Boolean column value of true in asp.net? 如何从数据库中选择具有某个字符串一部分值的所有行? - How do I select all rows from my database that have a value that is part of a certain string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM