我对php和mysql都有点陌生,我遇到了一个问题(我甚至不确定这是否可行),我的问题是:
我这里有张table:
我用这句话:
SELECT *
FROM (SELECT DATE_FORMAT(STR_TO_DATE(SPLIT_STRING(`date`,' ',1), '%m/%d/%Y'), '%Y-%m-%d') as month
FROM `automation-reports`
) as innerTable
WHERE MONTH(month) = 5
这给了我:
我希望能够 success
从包含它的行开始:
我甚至不确定这是否可行,因为现在该行返回了数据,但正如我所说,我是mysql的新手,所以我不确定其局限性。
1条答案
按热度按时间9rnv2umw1#
考虑到你的
date
列不是一个真正的DATETIME
类型,则需要使用STR_TO_DATE()
但是您可以使用更完整的日期字符串格式来返回完整的DATETIME
一下子就把所有的东西都抛在脑后。正确的格式字符串是'%m/%d/%Y %r'
,在哪里%r
是12小时的时间吗hh:mm:ss
然后AM/PM
. 使用这种格式,您可以将整个输出 Package 成MONTH()
要么在SELECT
或在WHERE
.或者代替
HAVING
你可以把整个表达式放进去WHERE
```...
WHERE MONTH(STR_TO_DATE(date, '%m/%d/%Y %r') = 5)
-- Add a DATETIME
ALTER TABLE
automation-reports
ADD realdate DATETIME;-- And fill it with dates parsed from your string column
UPDATE
automation-reports
SET realdate = STR_TO_DATE(date, '%m/%d/%Y %r');-- Drop the old column if you do not need both
ALTER TABLE
automation-reports
DROP date;-- And rename the new one to the old name
ALTER TABLE
automation-reports
CHANGE realdate date DATETIME;