简体   繁体   English

where子句基于条件

[英]where clause based on conditions

I have a query where I match a column. 我有一个查询,我匹配一列。 The value to be matched may be null or not null (some value exists). 要匹配的值可以为null或不为null(存在某个值)。 The problem arises when the matching takes place. 匹配发生时会出现问题。 In case value is present the matching like 如果值存在,则匹配为

table.column = somevalue

works fine, but in case the value is null, then the matching needs to be done as 工作正常,但如果值为null,则匹配需要完成

table.column is null

Is there some way that I can choose the condition being used in the WHERE clause based on the value? 有没有什么方法可以根据值选择WHERE子句中使用的条件?

Thanks in advance 提前致谢

What about ORing your WHERE clause? ORing你的WHERE子句怎么样?

SELECT
*
FROM table
WHERE ((table.column = 123) AND table.column IS NOT NULL))
OR    (table.column IS NULL)

Use a CASE statement, eg: 使用CASE语句,例如:

SELECT
  CASE
    WHEN mycol IS NULL
      THEN 'it is null'
    WHEN mycol = 'foo'
      THEN 'it is foo'
    ELSE 'it is something else'
  END AS col
FROM mytable;

(Note: I don't know what DB you are using, so "IS NULL", "ISNULL", "ISNULL()" might be needed above) (注意:我不知道你使用的是什么数据库,所以上面可能需要“IS NULL”,“ISNULL”,“ISNULL()”)

检查MySQL 控制流功能

Try using ISNULL(somevalue, 0). 尝试使用ISNULL(somevalue,0)。 Provided that your "somevalue" table.column does not have 0 is a valid value. 前提是您的“somevalue”table.column没有0是有效值。 Instead of 0 you may use any string/number that will never appear as a valid value in table.column 您可以使用任何在table.column中永远不会显示为有效值的字符串/数字,而不是0

In ms sql you could do 在ms sql中你可以做到

      declare @match int
      select * from tbl where coalesce(tblValue, @match) = @match

That way you will compare @match with itself when tblValue is null. 这样,当tblValue为null时,您将比较@match与自身。

Of course you can eliminate the parameter if it is null by switching the matching 当然,如果参数为null,则可以通过切换匹配来消除该参数

      select * from tbl where tblValue = coalesce(@match, tblValue) 

but the latter query may cause the query optimizer to neglect indexes on tblValue, so you should check the execution plan. 但后一个查询可能导致查询优化器忽略tblValue上的索引,因此您应该检查执行计划。

I used decode and case statement to solve this problem This has been done in oracle. 我使用decode和case语句来解决这个问题这已经在oracle中完成了。


    AND DECODE(TSBI.SHIP_TRAIL_STATUS, 
               'L', 
               'TRUE', 
               'C', 
               (SELECT CASE 
                    WHEN TSBI.UPD_ON + 10 >= SYSDATE THEN 
                         'TRUE' 
                     ELSE 
                          'FALSE' 
                      END 
                FROM DUAL)) = 'TRUE'

In this condition I have checked the SHIP_TRAIL_STATUS. 在这种情况下,我检查了SHIP_TRAIL_STATUS。 In case it returns 'L' the decode function returns TRUE. 如果它返回'L',则解码函数返回TRUE。 In case it returns 'C' the decode checks the UPD_ON value. 如果它返回'C',则解码检查UPD_ON值。 Accordingly this CASE statement return TRUE or FALSE. 因此,此CASE语句返回TRUE或FALSE。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM