These queries require the string to date conversion because Timestamp is stored as a string, and the logging application is unchangeable.
I have a select query that works perfectly ->
(SELECT main.user_id, main.Timestamp
FROM `user_table` main
WHERE STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CST %Y' ) <
(SELECT MAX(STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CST %Y' ))
FROM `user_table` sub
WHERE sub.user_id = main.user_id ))
This will select ALL BUT most recently added user id and timestamp from my table.
However, when I try to insert this into another table...like so ->
INSERT INTO user_table_temp (`user_id`, `Timestamp`)
(SELECT main.user_id, main.Timestamp
FROM `user_table` main
WHERE STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CST %Y' ) <
(SELECT MAX(STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CST %Y' ))
FROM `user_table` sub
WHERE sub.user_id = main.user_id ))
I get the following error ->
#1411 - Incorrect datetime value: 'Mon Mar 14 16:10:10 CDT 2011' for function str_to_date
The reason for this is because my Timestamps are stored in two formats. One will store as 'CST' during non-daylight-savings time, the other will store as 'CDT' during daylight-savings time. When the INSERT INTO SELECT gets to the first row that has the opposite of what I'm querying against, it will fail with the above message.
I have also tried to COALESCE them, which also works when just running the select ->
INSERT INTO user_table_temp (`user_id `, `Timestamp`)
(SELECT main.user_id , main.Timestamp
FROM `user_table` main
WHERE COALESCE(STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ), STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CST %Y' )) <
(SELECT MAX(COALESCE(STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ), STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CST %Y' )))
FROM `user_table` sub
WHERE sub.user_id = main.user_id ))
Why would this fail on an INSERT, but the SELECT would work?