我有一个场景,我需要从表中选择员工列表,其中可以通过最小生日和最大生日。 所以我需要的是这样的事情: -
Select all from employees where active=1 and
if (not @min_birthday is null and @max_birthday is null)
birthday > @min_birthday
else if (@min_birthday is null and not @max_birthday is null)
birthday < @max_birthday
else if (not @min_birthday is null and not @max_birthday is null)
birthday between (@min_birthday and @max_birthday)
有没有在单个sql语句中完成上述内容?
答案 0 :(得分:1)
where active = 1
and birthday between isnull(@min_birthday, birthday)
and isnull(@max_birthday, birthday)