我有一个正在使用的功能,我需要PostgreSQL db返回今天尚未完成调查的所有用户。
select distinct *
from users
left join survey_results
on users.user_id = survey_results.user_id
where customer_id = '9000'
and survey_results.created_at < (DATE_PART('year', survey_results.created_at) = (SELECT EXTRACT(YEAR FROM CURRENT_TIMESTAMP))
AND DATE_PART('month', survey_results.created_at) = (SELECT EXTRACT(MONTH FROM CURRENT_TIMESTAMP))
AND DATE_PART('day', survey_results.created_at) = (SELECT EXTRACT(DAY FROM CURRENT_TIMESTAMP)))
我正在使用左联接联接到我的调查结果表上,并按customer_id进行过滤,并在survey_results.created <今天进行了过滤,但我正在使用日期部分并提取以获取日期。如果有更好的方法可以做到这一点,那就是我所拥有的。
我在运行查询而不是结果时收到此输出。
ERROR: operator does not exist: timestamp with time zone < boolean
LINE 6: and survey_results.created_at < (DATE_PART('year', survey_re...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
SQL state: 42883
Character: 155
与其使用datepart,不如将其与current_date进行比较-
select distinct *
from users
left join survey_results
on users.user_id = survey_results.user_id
where customer_id = '9000'
and survey_results.created_at < CURRENT_DATE
如果您的survey_results.created_at列为时间戳,请使用date函数将其转换为日期,然后将其与current_date进行比较-
select distinct *
from users
left join survey_results
on users.user_id = survey_results.user_id
where customer_id = '9000'
and DATE(survey_results.created_at) < CURRENT_DATE
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句