gpt4 book ai didi

mysql - 当日期为空时,SQL 查询中的 Where 子句跳过日期过滤器

转载 作者:行者123 更新时间:2023-12-01 00:20:13 25 4
gpt4 key购买 nike

我的搜索查询的参数是名称、起始日期、截止日期。当我将空字符串或 null 传递给日期参数时,它应该只返回所有与名称匹配的数据。此代码对我不起作用。

WHERE name=@name 
AND
(CASE WHEN @_from IS not null
THEN
Servicedate<=@_from
END)

这也不适合我。

WHERE name = @name AND
Servicedate>=ISNULL((case when @_from='' then NULL else @_from),Servicedate)

谢谢。

最佳答案

你可以使用这个:

WHERE
name = @name
AND (@_from IS NULL OR ServiceDate >= @_from)
AND (@_to IS NULL OR ServiceDate <= @_to)

如果未设置变量(@_from@_to),条件将返回 true,因为 @var IS NULL true

关于mysql - 当日期为空时,SQL 查询中的 Where 子句跳过日期过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31691567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com