Search code examples
mysqlcorrelated-subquery

MySQL using current results for subquery. Correlated subquery?


I have a table that contains the following fields system_id partner_id uptime

I'm trying to get output that shows: system_id, uptime, partner_id, partner_uptime

So for every row that comes back from an initial select all I need to check if the partner id is in the table and retrieve it's uptime value. It's simple enough to do in excel but with 2M+ records it could take a while!

Can someone please help construct a basic query for this?

Thanks


Solution

  • You can use simple self join query here, assuming partner_id references to system_id:

    select t.system_id, t.uptime, t1.partner_id, t1.uptime as partner_uptime
    from table t join table t1 on t.system_id = t2.partner_id
    where //your condition