我正在尝试创建提供Childerens
ID的查询,重要的是我们有季节特定的儿童和中立。 Childrens
表有一个名为season
的字段,其值可能为0或2
0 means Neutral / Season independet
1 means only for Summer
2 means only for Winter
然后它有4个下一个字段summer_from
,summer_till
,winter_from
和winter_till
我需要获得所有Child对象,它们具有season = 0加上冬季或夏季特定对象。
这是我使用脚本语言动态生成的示例查询。我已尽可能删除了脚本代码。
有没有更好的方法来编写此查询?
SELECT id
FROM Childrens
WHERE id_parent= 2656
## if the parameter 'season' is 1 or 2 then we will access
if(season == 1)
AND Now() >= summer_from
AND Now() <= summer_till
elseif ( season == 2 )
AND Now() >= winter_from
AND Now() <= winter_till
UNION
SELECT id
FROM Childrens
WHERE id_parent= 2656
AND season = 0
AND (
( NOW() >= summer_from AND NOW() <= summer_till)
OR
( NOW() >= winter_from AND NOW() <= winter_till)
)
答案 0 :(得分:0)
查看您的查询后,我就看到了这一点:
您希望满足以下条件之一:
这可以缩短为两个选项:
所以,这就是我写这个查询的方式:
SELECT id
FROM Childrens
WHERE id_parent = 2656
AND ( ((season != 1) AND (Now() >= winter_from) AND (Now() <= winter_till))
OR ((season != 2) AND (Now() >= summer_from) AND (Now() <= summer_till)) )