I am trying to run this query against tickets
table. ticket_updates
table contains rows matching tickets.ticketnumber = ticket_updates.ticketnumber
I want to check for rows in tickets
where the last row in ticket_updates.datetime
is >= 1 hour ago.
The problem with the below is that it's picking up rows from ticket_updates
where datetime
is over 1 hour ago, because its in my WHERE clause, so it's completely ignoring the most recent row which in fact is only 10 minutes ago.
So I think I need to remote the datetime
from my WHERE clause, but I'm not sure what to add to make it work.
SELECT * FROM tickets WHERE
(
status = 'Pending Response' AND
ticketnumber IN
(
SELECT ticketnumber FROM ticket_updates WHERE
type = 'customer_reminder_flag' AND
datetime < NOW() - INTERVAL 2 DAY
)
) OR
(
status = 'Pending Completion' AND
ticketnumber IN (
SELECT ticketnumber FROM ticket_updates WHERE
type = 'update' AND
datetime < NOW() - INTERVAL 1 HOUR
ORDER BY datetime DESC
)
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…