how to ignore a condition in where clause

本秂侑毒 提交于 2019-12-02 09:41:02

问题


SELECT field1 FROM table1 WHERE field1 >= 4006 AND field1 < ( SELECT field1 FROM table WHERE field1 > 4006 AND field2 = false ORDER BY field1 LIMIT 1 )

i want the second condition (AND field1 <) to be ignore if the inner select returned no record.

Related to this topic


回答1:


Something like (untested!):

SELECT field1 FROM table1
WHERE field1 >= 4006 
  AND (field1 < (
    SELECT field1 
    FROM table
    WHERE field1 > 4006
          AND field2 = false
    ORDER BY field1 
    LIMIT 1
    )
    OR
    NOT EXISTS (
    SELECT field1 
    FROM table
    WHERE field1 > 4006
          AND field2 = false
    ORDER BY field1 
    )
)


来源:https://stackoverflow.com/questions/5540416/how-to-ignore-a-condition-in-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!