gpt4 book ai didi

Mysql 从子查询中选择不起作用

转载 作者:行者123 更新时间:2023-11-29 19:27:32 25 4
gpt4 key购买 nike

我想创建属于每个用户的订单总数和总收入的报告。

SELECT w.id, CONCAT_WS(' ', w.fname, w.sname) AS full_name, 
te.total_earnings, te.assigned_jobs
FROM users AS w
LEFT JOIN (
SELECT wr.user_id,
COUNT(o.order_id) AS assigned_jobs,
SUM(o.fee) AS total_earnings
FROM (
SELECT DISTINCT user_id, order_id, withdrawn
FROM work_records
) AS wr
LEFT JOIN orders o ON o.order_id = wr.order_id
WHERE wr.withdrawn IS NULL
AND o.verified != 'rejected'
) AS te ON te.user_id = w.id
WHERE w.status = 1


orders work_records
___________________ _________________________________
| order ID | fee | | id | order_id | fee | user_id |
------------------- ---------------------------------
| 334 | 425 | | 1 | 334 | 50 | 6 |
| 2 | 334 | 50 | 6 |

此查询适用于单个用户 ID。但如果我想获得所有用户的报告,那就不行了。

有什么建议吗,谢谢?

最佳答案

这是其他人的答案。然而查询速度却变慢了。但如果您有更快的查询,我们很乐意分享。

SELECT w.id, CONCAT_WS(' ', w.fname, w.sname) AS full_name, 
te.total_earnings, te.assigned_jobs
FROM users AS w
LEFT JOIN (
SELECT w.id,
SUM(work.earnings) AS total_earnings,
COUNT(work.order_id) AS assigned_jobs
FROM users AS w
LEFT JOIN (
SELECT wr.order_id, wr.writer_id, o.fee AS earnings
FROM work_records wr
LEFT JOIN orders o ON o.order_id = wr.order_id
WHERE wr.withdrawn IS NULL
AND o.verified = 'verified'
GROUP BY wr.order_id
) work ON work.writer_id = w.id
GROUP BY work.writer_id
) te ON te.id = w.id

关于Mysql 从子查询中选择不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42027834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com