SELECT ac.id,SUM( vtti.volunteer_time_value ) AS total_time FROM activity_check ac JOIN volunteer_time_table vtt ON vtt.activity_id = ac.id JOIN volunteer_time_table vtt ON vtt.id = vtti.volunteer_time_table_id WHERE ac.deleted = 0 vtt.deleted = 0 vtti.deleted = 0 ORDER BY total_time DESC LIMIT 10;哪里有问题
时间: 2023-06-23 22:08:33 浏览: 88
这段SQL语句有些语法错误。首先,在第3行和第4行中,都有JOIN volunteer_time_table vtt,这是重复定义了,应该修改其中一个为JOIN volunteer_time_table vtti。其次,在WHERE子句中,多个条件之间应该用AND或OR连接,这里缺少了这些连接符。最后,在SELECT语句中,应该在SUM函数后面添加AS total_time,以为这个计算的结果命名。因此,正确的SQL语句应该是:
```
SELECT ac.id, SUM(vtti.volunteer_time_value) AS total_time
FROM activity_check ac
JOIN volunteer_time_table vtt ON vtt.activity_id = ac.id
JOIN volunteer_time_table vtti ON vtti.id = vtt.volunteer_time_table_id
WHERE ac.deleted = 0 AND vtt.deleted = 0 AND vtti.deleted = 0
GROUP BY ac.id
ORDER BY total_time DESC
LIMIT 10;
```
这个语句的改动包括:修改第3行的JOIN语句,添加了GROUP BY子句,修改WHERE子句中多个条件之间的连接方式,添加了AS关键字。
阅读全文