【问题标题】:How to change the time zone on a query result如何更改查询结果的时区
【发布时间】:2021-10-28 20:07:22
【问题描述】:

我有以下按预期工作的查询 -

SELECT
max(CASE WHEN `label` = 'Order Number' THEN `response` else '' END)as `Order Number`,
max(CASE WHEN `label` = 'Date' THEN `response` else '' END)as `Date`,
max(CASE WHEN `label` = 'Creditor Name' THEN `response` else '' END)as `Creditor Name`,
max(CASE WHEN `label` = 'Purchaser' THEN `response` else '' END)as `Purchaser`,
max(CASE WHEN `label` = 'Farm' THEN `response` else '' END)as `Farm`,
max(CASE WHEN `label` = 'Quantity' THEN `response` else '' END)as `Quantity`,
max(CASE WHEN `label` = 'Description' THEN `response` else '' END)as `Description`,
max(CASE WHEN `label` = 'Allocation' THEN `response` else '' END)as `Allocation`
From `inspection_items`
where `template_id` = 'template_xxxx' and `type` not like 'section' and `type` not like 'information' and `type`not like 'element' and `type` not like 'dynamicfield'
group by `parent_id`, `audit_id`
order by `created_at`,`item_index`

但是我还需要更改我可以通过此查询执行的日期 -

SELECT `response`, Date_format(Convert_TZ(STR_TO_DATE(`response`, '%Y-%c-%dT%H:%i:%s'), '+00:00', '+10:00'), '%Y-%c-%d %H:%i:%s') as 'Date'
from `inspection_items`

但是,如何将这两个查询组合在一起以更改第一个查询结果中的日期?

【问题讨论】:

    标签: mysql date format subquery


    【解决方案1】:

    如果我对您的问题的理解正确,您希望在第一个查询中格式化 Date 字段,就像您在第二个查询中设置的格式一样。

    试试这个 -

    SELECT
    max(CASE WHEN `label` = 'Order Number' THEN `response` else '' END)as `Order Number`,
    max(CASE WHEN `label` = 'Date' THEN Date_format(Convert_TZ(STR_TO_DATE(`response`, '%Y-%c-%dT%H:%i:%s') else '' END)as `Date`,
    max(CASE WHEN `label` = 'Creditor Name' THEN `response` else '' END)as `Creditor Name`,
    max(CASE WHEN `label` = 'Purchaser' THEN `response` else '' END)as `Purchaser`,
    max(CASE WHEN `label` = 'Farm' THEN `response` else '' END)as `Farm`,
    max(CASE WHEN `label` = 'Quantity' THEN `response` else '' END)as `Quantity`,
    max(CASE WHEN `label` = 'Description' THEN `response` else '' END)as `Description`,
    max(CASE WHEN `label` = 'Allocation' THEN `response` else '' END)as `Allocation`
    From `inspection_items`
    where `template_id` = 'template_xxxx' and `type` not like 'section' and `type` not like 'information' and `type`not like 'element' and `type` not like 'dynamicfield'
    group by `parent_id`, `audit_id`
    order by `created_at`,`item_index`
    

    【讨论】:

    • 感谢您的回复。但是,当我尝试新查询时,我收到了一个 sql 错误 #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'else'' END)as Date, max(CASE WHEN label = 'Creditor Name' THEN response ' 在第 3 行附近使用正确的语法跨度>
    • 道歉 - 通过包含 Date_format 的整个第二个查询来修复。它工作得很好,所以谢谢。
    猜你喜欢
    • 2019-11-08
    • 2023-03-23
    • 2021-09-19
    • 2017-10-16
    • 2017-04-30
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多