首先,道歉可能无法正确表达问题的标题,我对SQL查询的了解并不多。
我有一个表格“停车”,上面有这样的栏目
| days_2 | from_hr | to_hr
"1,2,3,4,5" "08:00:00" "18:00:00"
"0,1,2,3,4,5,6" "08:00:00" "22:00:00"
...
...
我做了一个正常工作的下一个查询:
SELECT Extract(dow from current_timestamp::DATE) as Day,
current_time BETWEEN from_hr and to_hr as ParkHour,
days_2,
from_hr,
to_hr
FROM public.parkings;
如果今天是星期一,当前时间是21:26:00,结果是:
day | ParkHour | days_2 | from_hr | to_hr
1 f "1,2,3,4,5" "08:00:00" "18:00:00"
1 t "0,1,2,3,4,5,6" "08:00:00" "22:00:00"
...
...
我想修改此查询,以便在第一列中存储结果(true / false),如果当前日期编号位于该记录的表格列 days_2 中,在某种程度上像这样
SELECT Extract(dow from current_timestamp::DATE) in (1,2,3,4,5,6);
如果它现在是星期日(0),例如最终结果对于第一行将为false,但在第二行中为true:
day | ParkHour | days_2 | from_hr | to_hr
f f "1,2,3,4,5" "08:00:00" "18:00:00"
t t "0,1,2,3,4,5,6" "08:00:00" "22:00:00"
我该如何做到这一点?
谢谢!
答案 0 :(得分:1)
select
extract(dow from now())
=
any (regexp_split_to_array('1,2,3', ',')::integer[]);
?column?
----------
t
将该列类型转换为数组将避免字符串拆分步骤。
答案 1 :(得分:1)
尝试
select
case
when Extract(dow from current_timestamp::DATE) = '0' then 't' else 'f'
when Extract(dow from current_timestamp::DATE) = '1' then 't' else 'f'
when Extract(dow from current_timestamp::DATE) = '2' then 't' else 'f'
when Extract(dow from current_timestamp::DATE) = '3' then 't' else 'f'
when Extract(dow from current_timestamp::DATE) = '4' then 't' else 'f'
when Extract(dow from current_timestamp::DATE) = '5' then 't' else 'f'
when Extract(dow from current_timestamp::DATE) = '6' then 't' else 'f'
end as day,
current_time BETWEEN from_hr and to_hr as ParkHour,
days_2,
from_hr,
to_hr
from public.parkings;
正如其他人所提到的,如果您的表格结构更好和/或您使用的是数组会更容易。
HTH