6
Assuming the following query where we are selecting values, grouping them and then re-grouping the result:
SELECT
CONCAT (b.label, '|', b.slug)
FROM (
SELECT
group_concat(name SEPARATOR '$') AS label,
group_concat(slug SEPARATOR '$') AS slug
FROM (
SELECT
color.name,
color.slug
FROM color
INNER JOIN product__colors USING ( color_id )
WHERE product__colors.product_id = 1 -- integer fixo funciona
) AS a
) AS b
Output example:
azul$vermelho$branco verde|azul$vermelho$branco-verde
But the query is to perform with the value of product__colors.product_id
variable and not 1
as in the example above:
SELECT
CONCAT (b.label, '|', b.slug)
FROM (
SELECT
group_concat(name SEPARATOR '$') AS label,
group_concat(slug SEPARATOR '$') AS slug
FROM (
SELECT
color.name,
color.slug
FROM color
INNER JOIN product__colors USING ( color_id )
WHERE product__colors.product_id = ? -- integer variável não funciona
) AS a
) AS b
Question
How can we pass a variable value to the innermost condition ?
It was not very clear to me what exercise and what you want to move from query to subquery. I suspect that this query could be written differently to achieve the result you would like. But in advance, for this type of problem, where the subquery needs query information, it is usually used
JOIN
orWHERE
. See: http://www.geeksengine.com/database/subquery/correlated-subquery.php– Dherik