我的SQL TIME(STR_TO_DATE)函数不起作用

时间:2018-09-27 23:06:58

标签: mysql datetime

我正在尝试将时间值存储在数据库中,但是STR_TO DATE FUNCTION在mysql中不起作用,但是当我在w3school sql引擎上尝试时,它可以与相同格式正常工作。

INSERT INTO `discussions`( `Name`, `Topic`, `Author`, `Auth_ID`, `day`, `Date`, `Time`, `Activated`, `Topic_ID`) 
VALUES ('asdfgh','PCB Designing','ShahzaibAhmed',1,'Sunday','2018-10-28',Time(STR_TO_DATE( '03:57:AM', '%h:%i:%p' ) ),1,2);

这是我尝试时遇到的错误

enter image description here

此外,当我仅将其更改为日期时,它就可以正常工作并且可以正确插入而没有任何错误

INSERT INTO `discussions`( `Name`, `Topic`, `Author`, `Auth_ID`, `day`, `Date`, `Time`, `Activated`, `Topic_ID`) 
VALUES ('asdfgh','PCB Designing','ShahzaibAhmed',1,'Sunday','2018-10-28',Time(STR_TO_DATE( '20120801', '%Y%m%d' ) ),1,2)

enter image description here

1 个答案:

答案 0 :(得分:2)

似乎依赖于MySQL版本:

#include <stdio.h>
#include <stdlib.h>

typedef struct Employee
{
    int number;
    char name[20];
    char department[15];
    double salary;
}Employee;

void employeePrint(Employee arr[]);

main()
{
    Employee e1 = {101,"John Smith\0","Accounting\0",54926.25};
    Employee e2 = {102,"Jane Q. Public\0","Retail\0",54926.24};
    Employee e3 = {103,"George Washington\0","Tech\0",70417.76};

    Employee empArr[3] = {e1,e2,e3};
    employeePrint(empArr);
}

void employeePrint(Employee arr[])
{
    int i;
    for(i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
    {
        printf("Number: %-4d\n",arr[i].number);
        printf("Name: %-6s\n",arr[i].name);
        printf("Department: %s\n",arr[i].department);
        printf("Salary: $%-2.2lf",arr[i].salary);
        printf("----------------");
    }
}

解析时间MySQL-5.6,但不解析时间MySQL-5.7MySQL-8.0

MariaDB-10.3和10.2正确解析结果。

MySQL bug 71386强调说,sql_mode SELECT STR_TO_DATE( '03:57:AM', '%h:%i:%p' ) 是更高版本的MySQL版本中的默认设置,尽管只是一次,但仍有效。

MariaDB-10.3在设置=NO_ZERO_DATE时返回正确的结果事件。

因此,要解决您的错误,请在此查询之前的会话中禁用“ NO_ZERO_DATE”:

sql_mode=NO_ZERO_DATE