我有一个MySql查询,如下所示:
SELECT o.id,
o.priorytet_1 +
o.priorytet_2 +
o.priorytet_3 AS bonus
FROM obiekty o
WHERE o.ac=1
GROUP BY o.id
ORDER BY `o`.`id` ASC
priorytet_1, priorytet_2 and priorytet_3
列旁边的priorytet_1_valid, priorytet_2_valid, priorytet_3_valid
我存储了日期,例如priorytet_1
有效。
我想只添加那些日期为< now()
的列。
类似的东西:
SELECT o.id,
if (o.priorytet_1_valid < now()) o.priorytet_1 +
if (o.priorytet_2_valid < now()) o.priorytet_2 +
if (o.priorytet_3_valid < now()) o.priorytet_3 AS bonus
FROM obiekty o
WHERE o.ac=1
GROUP BY o.id
ORDER BY `o`.`id` ASC
感谢您的帮助!
答案 0 :(得分:0)
尝试:
SELECT o.id,
SUM(IF (o.priorytet_1_valid < NOW(), o.priorytet_1, 0) +
IF (o.priorytet_2_valid < NOW(), o.priorytet_2, 0) +
IF (o.priorytet_3_valid < NOW(), o.priorytet_3, 0)) AS bonus
FROM obiekty o
WHERE o.ac=1
GROUP BY o.id
ORDER BY `o`.`id` ASC
您可能希望使用SUM
,因为您有GROUP BY
子句。上面的查询中使用的SUM
函数有条件地会添加字段o.priorytet_1, o.priorytet_2, o.priorytet_3
,以防相应的有效&#39;字段早于当前日期/时间。