Jump to content

SQL query + subquery no funciona


Recommended Posts

Buenas a todos,

Estoy teniendo problemas al usar subqueries. Cuando intento la siguiente query + subquery/subtabla me da error:

WITH `table1` AS (
SELECT
odet.`product_id`,
SUM(odet.`product_quantity`) as product_quantity

FROM `zsdr_order_detail` odet
LEFT JOIN `zsdr_orders` os ON (odet.`id_order` = os.`id_order`)

GROUP BY 1
)

SELECT SQL_CALC_FOUND_ROWS 
p.active,
p.reference,
pl.name,
pl. description_short,
p.price,
p.id_tax_rules_group,
sa.`quantity`,
od.`product_quantity`


FROM  `zsdr_product` p 
LEFT JOIN `zsdr_product_lang` pl ON (pl.`id_product` = p.`id_product` AND pl.`id_lang` = 2)
LEFT JOIN `zsdr_stock_available` sa ON (sa.`id_product` = p.`id_product`) 
LEFT JOIN `table1` od ON (p.`id_product` = od.`product_id`)

;

 

Sin embargo, la siguiente query sin query, que debería dar el mismo resultado que al anterior, si que me funciona:

SELECT SQL_CALC_FOUND_ROWS 
p.active,
p.reference,
pl.name,
pl. description_short,
p.price,
p.id_tax_rules_group,
sa.`quantity`,
SUM(od.`product_quantity`) as product_quantity

FROM  `zsdr_product` p 
LEFT JOIN `zsdr_product_lang` pl ON (pl.`id_product` = p.`id_product` AND pl.`id_lang` = 2)
LEFT JOIN `zsdr_stock_available` sa ON (sa.`id_product` = p.`id_product`) 
LEFT JOIN `zsdr_order_detail` od ON (p.`id_product` = od.`product_id`)
LEFT JOIN `zsdr_orders` os ON (od.`id_order` = os.`id_order`)

GROUP BY 1,2,3,4,5,6,7
;

 

Lo cual me hace pensar que estoy creando la subquery/subtabla mal por alguna razón, ¿alguien sabría decirme donde puede estar el error? He probado mil maneras distintas, no hay forma de crear una subquery que me funciona.

 

Muchas gracias,

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...