Prestashop BestSelling产品SQL查询Snytax错误

时间:2016-01-15 12:51:30

标签: mysql prestashop prestashop-1.6

我正在使用Prestashop 1.6.0.9,我希望在2015年1月1日至2015年12月31日期间从我的数据库中检索我最好的产品。我写了一个查询,但是出现了错误。

这是我的问题:

SELECT
    SQL_CALC_FOUND_ROWS p.reference,
    p.id_product,
    pl.name,
    ROUND(AVG(od.product_price / o.conversion_rate), 2) as avgPriceSold,
    IFNULL(stock.quantity, 0) as quantity,
    IFNULL(SUM(od.product_quantity), 0) AS totalQuantitySold,
    ROUND(IFNULL(IFNULL(SUM(od.product_quantity), 0) / (1 + LEAST(TO_DAYS('01/01/2015'), TO_DAYS(NOW())) - GREATEST(TO_DAYS('31/12/2015'), TO_DAYS(product_shop.date_add))), 0), 2) as averageQuantitySold,
    ROUND(IFNULL(SUM((od.product_price * od.product_quantity) / o.conversion_rate), 0), 2) AS totalPriceSold,
    (
        SELECT IFNULL(SUM(pv.counter), 0)
        FROM ps_page pa
        LEFT JOIN ps_page_viewed pv
            ON pa.id_page = pv.id_page
        LEFT JOIN ps_date_range dr
            ON pv.id_date_range = dr.id_date_range
        WHERE
            pa.id_object = p.id_product
            AND dr.time_start BETWEEN '01/01/2015 AND 31/12/2015'
            AND dr.time_end BETWEEN '01/01/2015 AND 31/12/2015'
    ) AS totalPageViewed,
    ps_product_shop.active
FROM ps_product p
LEFT JOIN ps_product_lang pl
    ON (p.id_product = pl.id_product AND pl.id_lang = '3')
LEFT JOIN ps_order_detail od
    ON od.product_id = p.id_product
LEFT JOIN ps_orders o
    ON od.id_order = o.id_order
WHERE
    o.valid = 1
    AND o.invoice_date BETWEEN '01/01/2015 AND 31/12/2015'
GROUP BY od.product_id'

以下是错误消息:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近')AS totalPageViewed,                 ps_product_shop.active                 来自ps_product p                 LEFT'在第15行

2 个答案:

答案 0 :(得分:1)

在此查询中的许多错误中,确定的特定错误与您对BETWEEN的使用有关。

你应该写:

BETWEEN '01/01/2015' AND '31/12/2015'

而不是:

BETWEEN '01/01/2015 AND 31/12/2015'

此外,您使用的是stock.quantity,但查询中没有任何名为“stock”的内容。

答案 1 :(得分:0)

我自己找到答案:)

那就是我需要它:)

SELECT P.id_product as SKU, PL.`name` as Product_Name, COUNT(OD.id_order) as ordered_qty,ROUND(AVG(OD.product_price / O.conversion_rate), 2) as avgPriceSold,P.`wholesale_price`,COUNT(OD.id_order)*P.`wholesale_price` as Satis_Whosale_carpimi,COUNT(OD.id_order)*ROUND(AVG(OD.product_price / O.conversion_rate), 2) as Toplam_satis FROM ps_product P 
LEFT JOIN ps_product_lang PL ON P.id_product = PL.id_product
LEFT JOIN ps_order_detail OD ON P.id_product = OD.product_id
LEFT JOIN ps_orders O ON O.id_order = OD.id_order
WHERE O.valid = 1 AND PL.`id_lang`=1 AND O.invoice_date BETWEEN '2015-01-01 00:00:00' AND '2015-12-31 23:59:59'
GROUP BY P.reference, PL.`name`
ORDER BY ordered_qty DESC