Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL correlated subquery in JOIN syntax
    primarykey
    data
    text
    <p>I would like to provide a WHERE condition on an inner query by specifying innertable.id = outertable.id. However, MySQL (5.0.45) reports "Unknown column 'outertable.id' in 'where clause'". Is this type of query possible?</p> <p>The inner query is pivoting rows to columns using a GROUP BY. This could be entirely be performed in the outer query, but would possibly incur additional overhead due to the extra joins.</p> <p>Alternatively, I can leave off the WHERE condition in the inner query and instead specify an ON outertable.id = innerquery.id, but it would then fetch the entire inner query rowset to join again the outer, which is inefficient. </p> <p>The actual SQL appears below:</p> <pre><code>select t.ticketid, u.userid, t.fullname, u.loginapi_userid, t.email, tp.subject, tp.contents, a.PhoneNumber, a.Location, a.Extension, a.BusinessUnit, a.Department from swtickets t inner join swticketposts tp on t.ticketid = tp.ticketid inner join swusers u on t.userid = u.userid left join ( select cfv.typeid, min(case cfv.customfieldid when 1 then cfv.fieldvalue end) as 'PhoneNumber', min(case cfv.customfieldid when 3 then cfv.fieldvalue end) as 'Location', min(case cfv.customfieldid when 5 then cfv.fieldvalue end) as 'Extension', min(case cfv.customfieldid when 8 then cfv.fieldvalue end) as 'BusinessUnit', min(case cfv.customfieldid when 9 then cfv.fieldvalue end) as 'Department' from swcustomfieldvalues cfv where cfv.typeid = t.ticketid group by cfv.typeid ) as a on 1 = 1 where t.ticketid = 2458; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload