我可以在哪里找到Prestashop SQL查询,它们显示按顺序购买的产品。
我正在创建一个自定义页面,在表格中显示100个订单。如果点击订单,它会显示订购的产品。我做了一个SQL查询:
SELECT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,group_concat(ps.product_supplier_reference) as supp_ref, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
LEFT JOIN ps_product_lang pl on o.product_id = pl.id_product
LEFT JOIN ps_product p on p.id_product = pl.id_product
LEFT JOIN ps_stock_available psa on p.id_product = psa.id_product
LEFT JOIN ps_category_lang c on c.id_category = p.id_category_default
LEFT JOIN ps_product_supplier ps on p.id_product = ps.id_product
WHERE pl.id_lang=2 AND c.id_lang=2 AND id_order= '".$q."'
GROUP BY product_id, kood,nimetus,nr,pl.link_rewrite, kogukogus,asukoht, link
但是当产品具有属性时,它会复制产品。
在这种情况下,iButton有两种颜色黑色和红色(属性)。按此顺序,只有一个黑色的iButton以20的数量购买,但它显示了三行。
我试图解决这个问题,但是我的sql查询出现了另一个问题,即当它没有属性时它就不会显示任何内容。
SELECT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,group_concat(ps.product_supplier_reference) as supp_ref, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
JOIN ps_product_lang pl on o.product_id = pl.id_product
JOIN ps_product p on p.id_product = pl.id_product
JOIN ps_stock_available psa on p.id_product = psa.id_product
JOIN ps_category_lang c on c.id_category = p.id_category_default
JOIN ps_product_supplier ps on p.id_product = ps.id_product
LEFT JOIN ps_product_attribute pa on p.id_product=pa.id_product
WHERE pl.id_lang=2 AND pa.id_product_attribute = psa.id_product_attribute AND pa.id_product_attribute =o.product_attribute_id AND c.id_lang=2 AND id_order= '".$q."'
GROUP BY product_id, kood,nimetus,nr,pl.link_rewrite, kogukogus,asukoht, link
如何解决此问题?
答案 0 :(得分:1)
我认为您必须将WHERE
个条件中的2个移至JOIN
条件。可能只移动LEFT JOIN
上的条件就足够了。
这应该是结果:
SELECT DISTINCT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,group_concat(ps.product_supplier_reference) as supp_ref, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
JOIN ps_product_lang pl on o.product_id = pl.id_product
JOIN ps_product p on p.id_product = pl.id_product
JOIN ps_stock_available psa on p.id_product = psa.id_product
JOIN ps_category_lang c on c.id_category = p.id_category_default
JOIN ps_product_supplier ps on p.id_product = ps.id_product
LEFT JOIN ps_product_attribute pa on p.id_product=pa.id_product
AND psa.id_product_attribute = pa.id_product_attribute
WHERE pl.id_lang=2 AND c.id_lang=2 AND id_order= '".$q."'
答案 1 :(得分:0)
如果您对结果的特定产品属性不感兴趣,请在查询中使用SELECT DISTINCT而不是简单的SELECT
答案 2 :(得分:0)
感谢您的回答,这真的很愚蠢但是当从组中删除kogukogus并且结果如下:
SELECT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,group_concat(ps.product_supplier_reference) as supp_ref, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
LEFT JOIN ps_product_lang pl on o.product_id = pl.id_product
LEFT JOIN ps_product p on p.id_product = pl.id_product
LEFT JOIN ps_stock_available psa on p.id_product = psa.id_product
LEFT JOIN ps_category_lang c on c.id_category = p.id_category_default
LEFT JOIN ps_product_supplier ps on p.id_product = ps.id_product
WHERE pl.id_lang=2 AND c.id_lang=2 AND id_order= '".$q."'
GROUP BY product_id, kood,nimetus,nr,pl.link_rewrite,asukoht, link
我以某种方式工作了。谢谢你@ kiks73,你的代码没有用,但是当我试图将你的代码添加到你的代码时,我意识到问题出在group by上。所以赏金归于你,因为没有人回答任何严肃的事情