SQL 按 select 订购

SQL orderby select

我有 SQL 个查询。我想按父类别排序。但是我return只有默认类别。我想知道我是否可以

ORDER BY SELECT id_parent FROM cats WHERE id_category = id_default_category

这是我的查询:

SELECT cp.`id_product_attribute`, 
cp.`id_product`, cp.`quantity` AS cart_quantity, 
cp.id_shop, pl.`name`, 
p.`is_virtual`,
pl.`description_short`, 
pl.`available_now`, 
pl.`available_later`, 
product_shop.`id_category_default`, 
p.`id_supplier`,
p.`id_manufacturer`, 
product_shop.`on_sale`, 
product_shop.`ecotax`, 
product_shop.`additional_shipping_cost`,
product_shop.`available_for_order`, 
product_shop.`price`, 
product_shop.`active`, 
product_shop.`unity`, 
product_shop.`unit_price_ratio`,
stock.`quantity` AS quantity_available, 
p.`width`, 
p.`height`, 
p.`depth`, 
stock.`out_of_stock`, 
p.`weight`,
p.`date_add`, 
p.`date_upd`, 
IFNULL(stock.quantity, 0) as quantity, 
pl.`link_rewrite`, 
cl.`link_rewrite` AS category,
CONCAT(LPAD(cp.`id_product`, 10, 0), 
LPAD(IFNULL(cp.`id_product_attribute`, 0), 10, 0), 
IFNULL(cp.`id_address_delivery`, 0)) AS unique_id, 
cp.id_address_delivery,
product_shop.advanced_stock_management, 
ps.product_supplier_reference supplier_reference 
FROM `ps_cart_product` cp
LEFT JOIN `ps_product` `p` ON p.`id_product` = cp.`id_product`
INNER JOIN `ps_product_shop` product_shop ON (product_shop.`id_shop` = cp.`id_shop` AND product_shop.`id_product` = p.`id_product`)
LEFT JOIN `ps_product_lang` `pl` ON p.`id_product` = pl.`id_product`
AND pl.`id_lang` = 1 AND pl.id_shop = cp.id_shop 
LEFT JOIN `ps_category_lang` `cl` ON product_shop.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = 1 AND cl.id_shop = cp.id_shop 
LEFT JOIN `ps_product_supplier` `ps` ON ps.`id_product` = cp.`id_product` AND ps.`id_product_attribute` = cp.`id_product_attribute` AND ps.`id_supplier` = p.`id_supplier`
LEFT JOIN ps_sanishopstock_available stock
ON (stock.id_product = cp.id_product AND stock.id_product_attribute = IFNULL(`cp`.id_product_attribute, 0) AND stock.id_shop = 1  AND     stock.id_shop_group = 0  )
WHERE cp.`id_cart` = 757
ORDER BY product_shop.id_category_default ASC, cp.id_product, cp.date_add ASC;

有很多不同的table,我迷路了!

如果有人有任何想法。 非常感谢!

是的。但是像任何子查询一样,您需要括号:

ORDER BY (SELECT c.id_parent FROM cats c WHERE id_category = id_default_category)

我也会限定 WHERE 中的列名称,但我不知道它们来自哪里。