简体   繁体   English

SQL:其中列不等于返回结果,否则结果等于空白值

[英]SQL:where column not equal return result else result equals blank value

So I am coming across an issue with a sql query (mssql) I have been working on that has multi-statement where clause, and one of the where statements may or may not return a value. 所以我遇到了我一直在处理的SQL查询(mssql)问题,该查询具有多语句where子句,其中where语句之一可能返回也可能不返回值。 If the where condition is not met how can I have it return an empty value and still return the rest of my results? 如果不满足where条件,该如何返回一个空值并仍然返回其余结果? I am also using multiple CTEs. 我也在使用多个CTE。

Here is my select clause: 这是我的选择子句:

select cte_devinfo.SerialNumber,
    cte_devinfo.DeviceName,
    cte_devinfo.DeviceID, 
    dev_CTE.concurrencies,
    (cte_slots.LocationIndex +1) as 'Total Media',
    cte_changer.SlotCount, cte_changer.TotalMountErrors, cte_changer.TotalMounts,
    cte_mismatch.MismatchSerialNumber
from cte_devinfo, dev_CTE, cte_slots, cte_changer, cte_mismatch

Here is my where clause: 这是我的where子句:

where cte_devinfo.DeviceID = dev_CTE.DeviceParentID 
and cte_slots.LocationID = dev_CTE.DeviceParentID
and cte_changer.ChangerID = dev_CTE.DeviceParentID
and cte_mismatch.LocationID = dev_CTE.DeviceParentID 

I want to add something like this to my where clause: 我想在我的where子句中添加以下内容:

and cte_mismatch.MismatchSerialNumber != cte_devinfo.SerialNumber 

but this condition may never occur, and if it doesn't how can I ignore the condition and just return ' ' so the rest of the query will run? 但是这种情况可能永远不会发生,如果不能解决,我该如何忽略这种情况而只返回'',以便其余查询继续运行?

First, rewrite your query using ANSI joins: 首先,使用ANSI联接重写查询:

select cte_devinfo.SerialNumber,
    cte_devinfo.DeviceName,
    cte_devinfo.DeviceID, 
    dev_CTE.concurrencies,
    (cte_slots.LocationIndex +1) as 'Total Media',
    cte_changer.SlotCount, cte_changer.TotalMountErrors, cte_changer.TotalMounts,
    cte_mismatch.MismatchSerialNumber
from cte_devinfo
inner join dev_CTE on cte_devinfo.DeviceID = dev_CTE.DeviceParentID
left outer join cte_slots on cte_slots.LocationID = dev_CTE.DeviceParentID
left outer join cte_changer on cte_changer.ChangerID = dev_CTE.DeviceParentID
left outer join cte_mismatch on cte_mismatch.LocationID = dev_CTE.DeviceParentID 

I changed all joins except the first one to outer, to allow for missing records for slots, changers, and mismatches. 我将除第一个以外的所有联接更改为外部联接,以允许丢失插槽,更换器和不匹配记录。 dev_CTE remains required, though, because all tables are joined to the record from it. 但是,仍然需要dev_CTE ,因为所有表都已从中加入到记录中。

Now you can add a where clause like this: 现在,您可以添加一个where子句,如下所示:

WHERE cte_mismatch.MismatchSerialNumber IS NULL OR cte_mismatch.MismatchSerialNumber != cte_devinfo.SerialNumber

This condition allows for NULL in MismatchSerialNumber , or even for missing cte_mismatch record. 此条件允许MismatchSerialNumberNULL ,甚至允许缺少cte_mismatch记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server:如何使用2个条件查找结果:一个等于某列; 另一个不等于另一列 - SQL Server: How to find the result with 2 criteria: one equals certain column; the other doesn't equal another column sql-从表值等于某值的结果集中排除结果 - sql - exclude result from result set where a table value equals something SQL返回子项计数不等于列值的行 - SQL return rows where count of children does not equal column value SQL(MS SQL 服务器)返回列值作为乘法的结果 - SQL (MS SQL Server) return column value as the result of the multiplication Oracle-SQL查询以从另一个表中的列返回值,其中值等于另一个列 - Oracle - SQL Query to return a value from a column in another table where value equals a different column 动态返回一列值等于另一列值的所有SQL结果 - Dynamically return all SQL results where the value of one column equals the value of another column SQL查询选择多个行,其中'field'等于'value',然后添加与结果关联的其他id - SQL query select multiple rows where 'field' equals to 'value' then add the other ids associated with the result SQL其中任何列等于一个值 - SQL Where ANY column equals a value 在SQL中结果为空的情况下如何返回列值? - How to return a column value in case result is empty in SQL? 将空白结果设置为等于SQL Server中的特定字符串 - Set blank result equal to specific string in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM