如何在不同时区添加额外的列

时间:2019-03-29 11:49:40

标签: mysql

我创建了一个简单的SQL脚本。该脚本提供了最新的聊天记录,但我想查看多个时区。服务器所在的一个时区和我居住的一个时区。因此,我需要一个额外的列来将“ msg_time”转换为UTC + 1。我已经搜索了一段时间,但找不到解决方案。 有人知道如何将此添加到我的脚本吗?

SELECT `msg`, FROM_UNIXTIME(`chatlog`.`msg_time`), `client_name` 
FROM `chatlog` 
order by FROM_UNIXTIME(`chatlog`.`msg_time`) DESC

1 个答案:

答案 0 :(得分:1)

您可以添加如下新列:

words = ["leetcode", "leet", "code", "le", "et", "etcode", "de", "decode", "deet"]


class node():
    def __init__(self, letter, is_there_a_word_that_ends_here):
        self.letter = letter # not really used but it feels weird to not have it in class
        self.is_there_a_word_that_ends_here = is_there_a_word_that_ends_here
        self.children = dict()


# actually defining tree is redundant you can just merge tree and node class together, but maybe this is more explicit
class Tree():
    def __init__(self):
        self.head = node(None, False)

    def add(self, word, head=None):
        if head is None:
            head=self.head

        if word[0] not in head.children.keys():
            head.children[word[0]] = node(word[0], False)

        if len(word) == 1:
            head.children[word[0]].is_there_a_word_that_ends_here = True
        else:
            self.add(word[1:], head=head.children[word[0]])


words = sorted(words, key=lambda w: len(w))
results = dict()
tree = Tree()
for word in words:
    tree.add(word)


def add_word_to_result(head, current_node, words_left, combinations, words_passed):
    if words_left[0] in current_node.children.keys():
        # this does not have to happen because we call this function with words that are not in the list as well
        next_node = current_node.children[words_left[0]]
        if len(words_left) == 1 and next_node.is_there_a_word_that_ends_here:
            combinations.append(words_passed+words_left)
        elif next_node.is_there_a_word_that_ends_here:
            add_word_to_result(head, head, words_left[1:], combinations, words_passed+words_left[0]+",")
            add_word_to_result(head, next_node, words_left[1:], combinations, words_passed + words_left[0])
        else:
            add_word_to_result(head, next_node, words_left[1:], combinations, words_passed+words_left[0])


for word in words:
    results[word] = []
    add_word_to_result(tree.head, tree.head, word, results[word], "")

print(results)

# {'le': ['le'], 'et': ['et'], 'de': ['de'], 'leet': ['le,et', 'leet'], 'code': ['code'], 'deet': ['de,et', 'deet'], 'etcode': ['et,code', 'etcode'], 'decode': ['de,code', 'decode'], 'leetcode': ['le,et,code', 'le,etcode', 'leet,code', 'leetcode']}

这样,列SELECT `msg`, FROM_UNIXTIME(`chatlog`.`msg_time`) as `msg_time`, CONVERT_TZ(`chatlog`.`msg_time`, 'GMT', 'Europe/Paris') as `msg_time_local`, `client_name` FROM `chatlog` order by `msg_time` DESC 将是您的原始列,而msg_time是已转换的列。

您将需要在MySQL中配置正确的时区支持,并按照https://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html中的说明设置时区表。