Our server is having a hard time running this query. I was hoping someone could help me out by suggesting a way of re-writing it to get the same results without the sub query.
The purpose of the subquery is to use min in the where clause: I'd like only results returned with earliest co.created date.
So what I wish I could do is this:
SELECT
my.data AS Account_ID,
co.ID AS User_ID,
def.def_medium,
co.created
FROM (tables with associated joins)
WHERE MIN(co.created)
But I cannot do that so I came up with this:
SELECT
my.data AS Account_ID,
co.ID AS User_ID,
def.def_medium,
co.created
FROM
abc_emails.cid208 co
INNER JOIN abc_emails.def208 def ON def.eid = co.id
INNER JOIN abc_emails.my208 my ON my.eid = co.id AND my.my_id = 2765
INNER JOIN (SELECT
my.data AS Account_ID,
MIN(created) AS created
FROM
abc_emails.my208 my
INNER JOIN abc_emails.cid208 co ON co.id = my.eid
WHERE my_id = 2765
GROUP BY Account_ID
) b1 ON b1.Account_ID = my.data
GROUP BY Account_ID, User_ID, def_medium
But it's been running for a long time and, given my experience with this server, I'm about to get booted off with an error message telling me it's timed out.
Is it possible to do what I'm doing with no subquery?