Mysql 使用嵌套 Select 进行连接

标签 mysql sql select join

我试图使用此 SQL 获取每个卖家每年、每月和每天的所有订单总数。我仍然在思考连接,但据我所知,我认为这应该可行

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

但它给了我一个错误(#1064 - 第 9 行“ONorders.sellerid = sellers.sellerid GROUP BY sellers.username HAVING”附近)

最佳答案

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

问候

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

关于Mysql 使用嵌套 Select 进行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19461236/

相关文章:

在 ember 中选择不加载数据 - 有工作示例吗?

php - MySQL Fetch 在单个查询中连接具有多行的多个表

c# - 如何处理 SqlCommand 结果(行)?

mysql - 当列值不在表 B 中时,SQL 查询从表 A 中获取行并计算将存储在表 B 中的内容

php - 检查字符串是否包含来自数据库的短语

mysql - SELECT 是一次全部发生还是逐步发生

android - URL 连接 - 无连接

MYSQL INSERT 随机值在 1 - 5 之间

mysql - 在 node-mysql 中防止 SQL 注入(inject)的最佳实践是什么?

phpmyadmin Designer Tab 不同颜色的含义?