gpt4 book ai didi

mysql - 在同一表中跨越 2 列的日期范围之间进行记录

转载 作者:行者123 更新时间:2023-11-29 16:13:43 25 4
gpt4 key购买 nike

我必须获取跨两列的日期范围之间的记录。

between statusEnteredDate and newDate in the image(Please click here)

到目前为止,我已经尝试了下面的代码,但似乎没有考虑 newDate 列。

我希望该行也能在 newColumn 中具有值(value)

SELECT *, COALESCE(`status_data`.`statusEnteredDate`, `status_data`.`newDate`) as twinData,
count(*) as MY_COUNT
FROM `status_data`
WHERE (`status_data`.`statusEnteredDate` BETWEEN '2019/02/24 10:00' AND '2019/03/01 10:00'
OR `status_data`.`newDate` BETWEEN 'null' AND '2019/02/27 10:00')
GROUP By `recordUniqueID`
ORDER by `id` ASC

我需要在这里保留GROUP By,因为我还想要重复行的计数。

还有什么方法可以在 COALESCE 上的同一 SQL 查询中应用逻辑?因为通过这种方式我可以将两列合并在一起。如下所示:

SELECT *, COALESCE(`status_data`.`statusEnteredDate`, `status_data`.`newDate`) as twinData,
count(*) as MY_COUNT
FROM `status_data`
WHERE (`twinData` BETWEEN '2019/02/24 10:00' AND '2019/02/27 10:00')
GROUP By `recordUniqueID`
ORDER by `id` ASC

上面的代码没有给我想要的结果。任何帮助将不胜感激。

最佳答案

null 周围有单引号,因此它被解释为零。并且您没有正确处理 NULL 比较。我想你想要:

select . . .
from status_data sd
where (sd.statusEnteredDate >= '2019-02-24 10:00' and
sd.statusEnteredDate < '2019-03-01 10:00'
) and
(sd.newDate is null or
sd.newDate < '2019-02027 10:00'
)

关于mysql - 在同一表中跨越 2 列的日期范围之间进行记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55024504/

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