select tb.userId from ( select a._user_id as userId, CAST((FLOOR(IFNULL(b.star, 0) / 100) + FLOOR(IFNULL(b.flower, 0) / 50) + FLOOR(IFNULL(b.thumb, 0) / 5) + FLOOR(IFNULL(b.bean, 0))) - FLOOR(IFNULL(b.consumed_points, 0)) as signed) as sortOrder from divx_social.osf_team_members a left join divx_social.dcp_stat_user_achieve b on a._user_id = b.user_id and b.stat_time_type = 4 where a._project_id = 9408 and a._enabled = 1 and a._role_id = 6 and b.inst_id = 17 ) as tb order by sortOrder DESC;解释这段SQL
时间: 2024-03-27 10:36:50 浏览: 158
这是一段SQL查询语句,其作用是从多个表中查询符合条件的记录,并根据计算得到的排序值进行降序排序,最终返回用户ID。
具体解释如下:
1. 查询的表
```
divx_social.osf_team_members
divx_social.dcp_stat_user_achieve
```
2. 查询条件
```
a._project_id = 9408 -- 对应osf_team_members表的项目ID为9408
a._enabled = 1 -- 对应osf_team_members表的启用状态为1
a._role_id = 6 -- 对应osf_team_members表的角色ID为6
b.inst_id = 17 -- 对应dcp_stat_user_achieve表的实例ID为17
b.stat_time_type = 4 -- 对应dcp_stat_user_achieve表的时间类型为4
```
3. 查询的字段
```
a._user_id as userId -- osf_team_members表中的_user_id字段,取别名为userId
```
4. 排序方式
```
CAST((FLOOR(IFNULL(b.star, 0) / 100) + FLOOR(IFNULL(b.flower, 0) / 50) + FLOOR(IFNULL(b.thumb, 0) / 5) + FLOOR(IFNULL(b.bean, 0))) - FLOOR(IFNULL(b.consumed_points, 0)) as signed) as sortOrder -- 计算得到的排序值,取别名为sortOrder,并按照该字段进行降序排序
```
5. 最终结果
```
SELECT tb.userId -- 返回userId字段
FROM (
-- 子查询,查询符合条件的记录,并计算排序值
SELECT a._user_id as userId, CAST((FLOOR(IFNULL(b.star, 0) / 100) + FLOOR(IFNULL(b.flower, 0) / 50) + FLOOR(IFNULL(b.thumb, 0) / 5) + FLOOR(IFNULL(b.bean, 0))) - FLOOR(IFNULL(b.consumed_points, 0)) as signed) as sortOrder
FROM divx_social.osf_team_members a
LEFT JOIN divx_social.dcp_stat_user_achieve b ON a._user_id = b.user_id AND b.stat_time_type = 4
WHERE a._project_id = 9408 AND a._enabled = 1 AND a._role_id = 6 AND b.inst_id = 17
) AS tb
ORDER BY sortOrder DESC; -- 按照排序值进行降序排序
阅读全文