Note that there are some explanatory texts on larger screens.

plurals
  1. POMySql scoping problem with correlated subqueries
    primarykey
    data
    text
    <p>I'm having this Mysql query, It works:</p> <pre><code>SELECT nom ,prenom ,(SELECT GROUP_CONCAT(category_en) FROM (SELECT DISTINCT category_en FROM categories c WHERE id IN (SELECT DISTINCT category_id FROM m3allems_to_categories m2c WHERE m3allem_id = 37) ) cS ) categories ,(SELECT GROUP_CONCAT(area_en) FROM (SELECT DISTINCT area_en FROM areas c WHERE id IN (SELECT DISTINCT area_id FROM m3allems_to_areas m2a WHERE m3allem_id = 37) ) aSq ) areas FROM m3allems m WHERE m.id = 37 </code></pre> <p>The result is:</p> <pre><code>nom prenom categories areas Man Multi Carpentry,Paint,Walls Beirut,Baalbak,Saida </code></pre> <p>It works correclty, but only when i hardcode into the query the id that I want (37). I want it to work for all entries in the m3allem table, so I try this:</p> <pre><code>SELECT nom ,prenom ,(SELECT GROUP_CONCAT(category_en) FROM (SELECT DISTINCT category_en FROM categories c WHERE id IN (SELECT DISTINCT category_id FROM m3allems_to_categories m2c WHERE m3allem_id = m.id) ) cS ) categories ,(SELECT GROUP_CONCAT(area_en) FROM (SELECT DISTINCT area_en FROM areas c WHERE id IN (SELECT DISTINCT area_id FROM m3allems_to_areas m2a WHERE m3allem_id = m.id) ) aSq ) areas FROM m3allems m </code></pre> <p>And I get an error:</p> <blockquote> <p>Unknown column 'm.id' in 'where clause'</p> </blockquote> <p>Why? From the MySql manual:</p> <pre><code>13.2.8.7. Correlated Subqueries [...] Scoping rule: MySQL evaluates from inside to outside. </code></pre> <p>So... do this not work when the subquery is in a SELECT section? I did not read anything about that.</p> <p>Does anyone know? What should I do? It took me a long time to build this query... I know it's a monster query but it gets what I want in a single query, and I am so close to getting it to work!</p> <p>Can anyone help?</p>
    singulars
    1. This table or related slice is empty.
    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.
    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