How would you do a inner join on a table where i need to filter the status, but on one of the status value, i need to perform an additional date comparison check on SQL phpmyadmin.
i have the following:
ex.
INNER JOIN orders backup_orders
ON backup_orders.bc_id = product_sold.order_id
AND backup_orders.status_id NOT IN (0, 1, 4, 6, 13, 14)
AND CONVERT_TZ( STR_TO_DATE( backup_orders.date_modified, "%a, %d %b %Y %T" ) , "GMT", "America/Chicago" )
>= CONVERT_TZ(STR_TO_DATE(:previous_week_start, "%a, %d %b %Y %T") , "GMT", "America/Chicago")
This only gets me the record where the order is cancelled and it was cancelled after the previous_week_start. I want all the records but if the status is 5 which is cancelled, perform the date comparison and if it was cancelled after the date then add it to the result.
#Edited:
AND(backup_orders.status_id NOT IN (0,1,4,6,13,14) OR (backup_orders.status_id = 5 AND CONVERT_TZ( STR_TO_DATE( backup_orders.date_modified, '%a, %d %b %Y %T' ) , 'GMT', 'America/Chicago' ) >= CONVERT_TZ(STR_TO_DATE("Thu, 11 Mar 2021 22:00:00 +0000", '%a, %d %b %Y %T') , "GMT", "America/Chicago")))
You can use case in where condition:
INNER JOIN orders backup_orders
ON backup_orders.bc_id = product_sold.order_id
and backup_orders.status_id NOT IN (0, 1, 4, 6, 13, 14)
AND (case when backup_orders.status_id =5 then
CONVERT_TZ( STR_TO_DATE( backup_orders.date_modified, "%a, %d %b %Y %T" ) , "GMT", "America/Chicago" )
>= CONVERT_TZ(STR_TO_DATE(:previous_week_start, "%a, %d %b %Y %T") , "GMT", "America/Chicago") end)
But wouldn't it better to compare backup_orders.status_id=5 directly instead of backup_orders.status_id NOT IN (0, 1, 4, 6, 13, 14)
If you examine the conditions in your edit, the date comparison never matters...
AND
(
backup_orders.status_id NOT IN (0,1,4,6,13,14)
OR
(
backup_orders.status_id = 5
AND
CONVERT_TZ( STR_TO_DATE( backup_orders.date_modified, '%a, %d %b %Y %T' ) , 'GMT', 'America/Chicago' )
>=
CONVERT_TZ(STR_TO_DATE("Thu, 11 Mar 2021 22:00:00 +0000", '%a, %d %b %Y %T') , "GMT", "America/Chicago")
)
)
If the status_id is 5, the first condition is True; 5 is NOT IN that list.
Then, it doesn't matter what the 2nd or 3rd conditions are, the result of TRUE OR (X AND Y) is True, no matter what X or Y are.
So, you need to add 5 to your list...
AND
(
backup_orders.status_id NOT IN (0,1,4,5,6,13,14) -- added 5 here
OR
(
backup_orders.status_id = 5
AND
CONVERT_TZ( STR_TO_DATE( backup_orders.date_modified, '%a, %d %b %Y %T' ) , 'GMT', 'America/Chicago' )
>=
CONVERT_TZ(STR_TO_DATE("Thu, 11 Mar 2021 22:00:00 +0000", '%a, %d %b %Y %T') , "GMT", "America/Chicago")
)
)
Now, a status of 5 can only resolve True if the date comparison Also resolves to True.
(Please forgive typos, I'm doing this on a phone.)
How do you convert the following date Mar 7 2017 1:26:46:886AM to 2017-03-07 01:26:00
select DATE_FORMAT(STR_TO_DATE('Mar 7 2017 1:26:46:886AM', '%b-%d-%Y'), '%Y-%m-%d');
I keep getting null
This is the answer
SELECT STR_TO_DATE("Mar 7 2017 1:26:46:886AM", "%M %d %Y %H:%i:%S")
you could use
select STR_TO_DATE('Mar 7 2017 1:26:46:886AM', '%b %d %Y %h:%i:%s%f' )
I'm trying to select data between july 2017 to jun 2018 using sql query but not getting expected output. Let's say m having data from jan 2017 up to dec 2018,
and i want to select data between july 2017 to jun 2018. how can I achieve this?
This is the data i'm having
And Expected Output is
See MySQL STR_TO_DATE() Function
SELECT * FROM <YOUR TABLE> WHERE STR_TO_DATE(CONCAT('1 ',`Month`,' ',`Year`), '%d %M %Y')
BETWEEN STR_TO_DATE('1 Jun 2017', '%d %b %Y') AND STR_TO_DATE('31 Jul 2017', '%d %b %Y');
What is wrong in
select str_to_date ( '%e %b %Y','14 Aug 1987' )
I even tried '%d %b %Y'
Referred http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
because it is "STRING" to "DATE" ... put the string first ...
select str_to_date ( '14 Aug 1987','%e %b %Y' )
I have developed a query to purge all but the most recently added row, based on a field -> Timestamp that is stored as text, and a User ID field.
Also involved is a changing date format, so some coalescing is involved as well.
Here is the delete query ->
DELETE
FROM `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 `table` sub
WHERE sub.Retrieving_User = main.Retrieving_User )
This doesn't run, throwing this error ->
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'main WHERE COALESCE(STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ),' at line 2
I understand what this is, just not why it is occuring. If I change the query to a select ->
SELECT *
FROM `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 `table` sub
WHERE sub.Retrieving_User = main.Retrieving_User )
it correctly grabs all the data I want to delete.
Please tell me I don't have to change the format of the query, because I've spent all morning on it. :(
EDIT:
I have found through research that you cannot delete from the same table you are using in your subquery. So I get to create a long workaround for this...will be updating later...
DELETE from `table` where COALESCE(STR_To_DATE( Timestamp , '%a %b %d %H:%i:%s CDT %Y' ), STR_To_DATE( Timestamp , '%a %b %d %H:%i:%s CST %Y' )) in
(
SELECT 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' ))
FROM `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 `table` sub
WHERE sub.Retrieving_User = main.Retrieving_User)
)