简体   繁体   English

MySQL加入嵌套选择

[英]Mysql Joins with nested Select

I am trying to get the total of all orders of each seller per year, month, and day using this sql. 我正在尝试使用此sql每年,每月和每天获取每个卖方的所有订单的总数。 I am still wrapping my head around joins, but from what I know i thought this should work 我仍在围绕着联接工作,但据我所知,我认为这应该可行

SELECT sellers.username, sellers.registerDate, sellers.sellerid,
orders.orderPrice, orders.orderDate, orders.sellerid,
count(orders.orderPrice) AS products, SUM( orders.orderPrice ) AS total,
FROM  `sellers` 
JOIN
`orders`,
(SELECT SUM( orders.orderPrice ) FROM  `orders` WHERE YEAR( orderDate ) = YEAR( CURDATE( ) ) ) AS year,
(SELECT SUM( orders.orderPrice ) FROM  `orders` WHERE MONTH( orderDate ) = MONTH( CURDATE( ) ) ) AS month,
(SELECT SUM( orders.orderPrice ) FROM  `orders` WHERE orderDate = CURDATE() ) AS day
ON orders.sellerid = sellers.sellerid
GROUP BY sellers.username
HAVING total > 0
ORDER BY total desc
LIMIT 0 , 4

But it gives me an error (#1064 - near 'ON orders.sellerid = sellers.sellerid GROUP BY sellers.username HAVING' at line 9) 但这给了我一个错误(#1064-在第9行的'ON order.sellerid = Sellers.sellerid GROUP BY Sellers.username HAVING'附近)

You need to set ON clause on every join table not only the lastone. 

Regards 问候

JOIN
    `orders` ON ??? = ???,
    (SELECT SUM( orders.orderPrice ) FROM  `orders` WHERE YEAR( orderDate ) = YEAR(   CURDATE( ) ) ) AS year ON orders.sellerid = sellers.sellerid,
    (SELECT SUM( orders.orderPrice ) FROM  `orders` WHERE MONTH( orderDate ) = MONTH( CURDATE( ) ) ) AS month ON orders.sellerid = sellers.sellerid,
    (SELECT SUM( orders.orderPrice ) FROM  `orders` WHERE orderDate = CURDATE() ) AS day 
         ON orders.sellerid = sellers.sellerid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM