【发布时间】:2017-01-05 19:42:48
【问题描述】:
当有必要时,我在我的数据库中使用timestamp(3) with time zone 作为表格,这在我的情况下几乎是任何情况。
我需要在比以下更复杂的查询中选择表的所有列。
我的问题是如何使用 SQL at time zone '<TIMEZONE>' 获取所需时区的时间戳(带时区),以获得像 q.* 这样的选择表达式,其中之一是时间戳(带时区)列。
我可能有相同情况的子查询。
是否有表达式可以在查询范围内实现这一点?
SELECT
q.*, -- created_at timestamp (with time zone) is already in here
q.created_at AT TIME ZONE 'EET', --instead of this redundant column selection
u.name AS author,
u.reputation,
CASE WHEN count(t.*)=0 THEN '[]' ELSE json_agg(t.*) END as tags
FROM posts q
-- authors
JOIN users u
ON q.author_id = u.id
-- tags
left join post_has_tag p_h_t
on q.id = p_h_t.post_id
left join tags t
on p_h_t.tag_id = t.id
WHERE q.post_type = 'question'
group by q.id, u.id;
【问题讨论】:
标签: sql postgresql timezone timestamp