能不能转换成sql查询语句发给我
时间: 2023-07-13 12:20:46 浏览: 79
从零开始学习SQL查询语句执行顺序
可以的,以下是优化后的 SQL 查询语句:
```sql
SELECT `id`
FROM `users`
LEFT JOIN `advisers` ON `users`.`adviser_id` = `advisers`.`id`
LEFT JOIN `job_logs` ON `users`.`id` = `job_logs`.`user_id`
LEFT JOIN `journals` ON `users`.`id` = `journals`.`user_id`
LEFT JOIN (
SELECT `user_id`,
COUNT(*) AS num,
SUM(CASE WHEN `status` = 2 THEN 1 ELSE 0 END) AS appropriation_num,
SUM(CASE WHEN `status` = 2 THEN `appropriation_amount` ELSE 0 END) AS appropriation_sum,
SUM(CASE WHEN `status` = 5 THEN 1 ELSE 0 END) AS net_income_num,
SUM(CASE WHEN `status` = 5 THEN `net_income` ELSE 0 END) AS net_income_sum
FROM `orders`
GROUP BY `user_id`
) AS `order` ON `users`.`id` = `order`.`user_id`
WHERE `id` = ?
LIMIT 1
```
这个 SQL 查询语句中包含了四个左联接和一个子查询,用于检索用户及其关联模型的信息。其中,子查询用于计算订单模型的统计信息,包括数量、总和等。最后,WHERE 子句用于根据用户的 ID 进行过滤。
阅读全文