如何在 SQL 钻取查询中检查表中的列退出

问题描述

SELECT t1.customerinformation.customerid AS CustomerId
FROM EndUserNotificationHistoryEvent t1
WHERE t1.operationType <> 'DELETE';

在上面的查询中,仅当表中存在 operationType 列时才应执行条件。如果在不需要条件的情况下不存在 operationType 列。

解决方法

对于此特定查询,您可以使用:

SELECT t1.customerInformation.customerid AS CustomerId
FROM EndUserNotificationHistoryEvent t1
WHERE t1.operationType <> 'DELETE' OR t1.operationType IS NULL;

向 where 子句添加 IS NULL 检查将有效关闭删除检查,如果列是 NULL

,

试试这个:

SELECT t1.customerInformation.customerid AS CustomerId
FROM EndUserNotificationHistoryEvent t1
WHERE (t1.operationType <> 'DELETE' OR t1.operationType IS NULL)
,

除了布尔运算,另一种方法是使用 COALESCE 如下:

SELECT T1.CUSTOMERINFORMATION.CUSTOMERID AS CUSTOMERID
  FROM ENDUSERNOTIFICATIONHISTORYEVENT T1
 WHERE COALESCE(T1.OPERATIONTYPE,'NOT DELETE') <> 'DELETE'; 
-- WHEN T1.OPERATIONTYPE IS NULL,CONDITION WILL BE ALWAYS TRUE

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...