gpt4 book ai didi

mysql - 用于日期/时间比较操作的归类的非法混合

转载 作者:行者123 更新时间:2023-11-29 02:31:48 25 4
gpt4 key购买 nike

我收到这个错误

Illegal mix of collations for operation <=

对于以下 WHERE 子句:

time(table1.dt1)<=date_add(table1.tim,interval 60 second)

dt1 = 日期时间, tim = 时间

谁知道为什么?

最佳答案

  1. DATE_ADD() 的手册条目中所述:

    The return value depends on the arguments:

    • DATETIME if the first argument is a DATETIME (or TIMESTAMP) value, or if the first argument is a DATE and the unit value uses HOURS, MINUTES, or SECONDS.

    • String otherwise.

    在您的例子中,因为 table1.tim 是一个 TIME 值,所以该函数返回一个字符串。

    字符串以character_set_connection给出的字符集和排序规则返回和 collation_connection .

  2. TIME() 的手册条目中所述:

    Extracts the time part of the time or datetime expression expr and returns it as a string.

    字符串作为二进制字符串返回(为什么不使用连接字符集和排序规则超出了我的理解范围——也许是一个错误?)。

  3. Type Conversion in Expression Evaluation 的手册条目中所述:

    If both arguments in a comparison operation are strings, they are compared as strings.

    因此,进行的比较是字符串比较,但不能将 binary 字符串强制转换为合适的排序规则以进行比较(因为其编码未知)。因此,您会看到错误。

可以通过显式 casting 强制正确进行比较一个或两个操作数,但我很想修改该子句,以便通过与 TIME 类型的比较隐式地进行转换:

TIME(table1.dt1 - INTERVAL 1 MINUTE) <= table1.tim

关于mysql - 用于日期/时间比较操作的归类的非法混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12052254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com