2016-04-01 17 views
0

Hallo ich mit einer Abfrage arbeite, aber es gibt Fehler:SQL-Fehler - beitreten

Die Abfrage sieht wie folgt aus:

SELECT 
    cd.*, 
    MIN(tmp.date_added) AS date_start, 
    MAX(tmp.date_added) AS date_end, 
    COUNT(tmp.order_id) AS `orders`, 
    SUM(tmp.products) AS products, 
    SUM(tmp.tax)  AS tax, 
    SUM(tmp.total)  AS total 
FROM 
    (
    SELECT 
    o.order_id, 
    (
     SELECT SUM(op.quantity) 
     FROM `order_product` op 
     WHERE op.order_id = o.order_id 
     GROUP BY op.order_id 
    ) AS products, 
    (
     SELECT SUM(ot.value) 
     FROM `order_total` ot 
     WHERE ot.order_id = o.order_id AND ot.code = 'tax' 
     GROUP BY ot.order_id 
    ) AS tax, 
    o.total, 
    o.date_added 
    FROM `order` o 
    WHERE o.order_status_id > '0' 
     AND DATE(o.date_added) >= '2016-01-01' 
     AND DATE(o.date_added) <= '2016-04-01' 
    GROUP BY o.order_id 
) tmp 
GROUP BY WEEK(tmp.date_added), 
    (
    SELECT ROUND(sum(op.total)) AS total 
    FROM order_product op 
     JOIN order o ON o.order_id = op.order_id 
     JOIN product_to_category p2c ON op.product_id = p2c.product_id 
    WHERE o.date_added > '2016-01-01 00:00:00' 
     AND o.date_added < '2016-03-28 23:59:59' 
     AND o.order_status_id = 3 
     AND p2c.category_id = cd.category_id 
) AS total 
FROM category_description cd 
JOIN category c ON c.category_id = cd.category_id 
WHERE c.status=1 
AND cd.language_id=1 
ORDER BY cd.category_id ASC; 

Aber es gibt mir diese Fehlermeldung:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as total from category_description cd join category c on c.categ' at line 16

Kann jemand sagen, wie man es repariert, danke.

+0

Warum nicht einfach verwenden 'ROUND (sum (op.total)) als total'? Die Unterabfrage dient keinem Zweck. –

+0

Wäre hilfreich, wenn Sie Ihre Abfrage so formatiert haben, dass sie einfacher zu lesen ist. –

+1

Auf den ersten Blick sieht es so aus, als wäre das mehr als ein Syntaxproblem, Ihre Abfrage ergibt keinen Sinn. Es scheint, dass Ihre äußere Auswahl zwei FROM-Klauseln hat. –

Antwort

-1

Bitte versuchen Sie unter Abfrage:

SELECT cd.*, 
MIN(tmp.date_added) AS date_start, 
MAX(tmp.date_added) AS date_end, 
COUNT(tmp.order_id) AS `orders`, 
SUM(tmp.products) AS products, 
SUM(tmp.tax) AS tax, 
SUM(tmp.total) AS total, 
(SELECT o.order_id, (SELECT SUM(op.quantity) FROM `order_product` op 
WHERE op.order_id = o.order_id GROUP BY op.order_id) AS products, 
(SELECT SUM(ot.value) FROM `order_total` ot WHERE ot.order_id = o.order_id AND ot.code = 'tax' 
GROUP BY ot.order_id) AS tax, 
(SELECT o.total, o.date_added FROM `order` o 
WHERE o.order_status_id > '0' AND DATE(o.date_added) >= '2016-01-01' 
AND DATE(o.date_added) <= '2016-04-01' GROUP BY o.order_id,WEEK(tmp.date_added)) AS tmp, 
(select ROUND(sum(op.total)) as total from order_product op 
join order o on o.order_id = op.order_id 
join product_to_category p2c on op.product_id = p2c.product_id 
where o.date_added > '2016-01-01 00:00:00' 
and o.date_added < '2016-03-28 23:59:59' 
and o.order_status_id = 3 
and p2c.category_id = cd.category_id) as total 
from category_description cd join category c on c.category_id = cd.category_id 
where c.status=1 and cd.language_id=1 order by cd.category_id asc 
+0

Sie haben einen Fehler in Ihrer SQL-Syntax. Überprüfen Sie das Handbuch, das Ihrer MySQL-Serverversion entspricht, für die richtige Syntax in der Nähe von 'GROUP BY WEEK (tmp.date_added), (wählen Sie ROUND (Summe (op.total)) als Summe aus Bestellung' in Zeile 14) – TheFermat