IFNULL(tpirdm.mcount ,0)
时间: 2024-05-21 19:16:38 浏览: 11
这是一段 SQL 语句,用于判断 tpirdm.mcount 是否为 NULL,如果是则返回 0,否则返回 mcount 的值。其中 IFNULL 是 MySQL 函数,其语法为 IFNULL(expr1, expr2),如果 expr1 不为 NULL,则返回 expr1,否则返回 expr2。所以这段语句的意思就是:如果 tpirdm.mcount 不为 NULL,则返回 tpirdm.mcount 的值,否则返回 0。
相关问题
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
这是一段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; -- 按照排序值进行降序排序
SELECT cs.taskname, rt.taskcont, rp.YEAR, rp.taskcont rpname, rp.iszb, ni.id, ifnull( ni.totalfinance, '0' ) totalfinance, ifnull( ni.centralfinance, '0' ) centralfinance, ifnull( ni.localfinance, '0' ) localfinance, ifnull( ni.investedfinance, '0' ) investedfinance, ifnull( ni.supportfinance, '0' ) supportfinance, ifnull( ni.selffinance, '0' ) selffinance, ni.statusnum FROM nk_xjsg_constask cs JOIN nk_xjsg_refinetask rt ON cs.id = rt.taskid AND cs.customerid = rt.customerid JOIN nk_xjsg_refinetask_process rp ON rt.id = rp.rtaskid AND rt.customerid = rp.customerid LEFT JOIN nk_xjsg_itembudget ni ON ni.rpid = rp.id AND ni.customerid = rp.customerid join nk_xjsg_process_leader nl on nl.itemid = rp.id and nl.customerid = rp.customerid join nk_user_info nui on nui.id = nl.tid and nui.customerid = nl.customerid where cs.pid = '40289e8c87fff7f80188042c15d50010' and ni.flowtaskid in ('40289e8b87a7c0ef0188527f09700206','40289e8b87a7c0ef018852359f600204') where cs.pid = '40289e8c87fff7f80188042c15d50010' > 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where cs.pid = '40289e8c87fff7f80188042c15d50010'' at line 33 哪里错了
根据错误提示,可以看出问题出在第二个 `where` 子句上。在 SQL 语句中,每个 `where` 子句应该只出现一次。因此,您需要删除第二个 `where` 子句,并将其与第一个 `where` 子句合并。合并后的 SQL 语句如下所示:
```
SELECT
cs.taskname,
rt.taskcont,
rp.YEAR,
rp.taskcont rpname,
rp.iszb,
ni.id,
ifnull(ni.totalfinance, '0') totalfinance,
ifnull(ni.centralfinance, '0') centralfinance,
ifnull(ni.localfinance, '0') localfinance,
ifnull(ni.investedfinance, '0') investedfinance,
ifnull(ni.supportfinance, '0') supportfinance,
ifnull(ni.selffinance, '0') selffinance,
ni.statusnum
FROM
nk_xjsg_constask cs
JOIN nk_xjsg_refinetask rt ON cs.id = rt.taskid AND cs.customerid = rt.customerid
JOIN nk_xjsg_refinetask_process rp ON rt.id = rp.rtaskid AND rt.customerid = rp.customerid
LEFT JOIN nk_xjsg_itembudget ni ON ni.rpid = rp.id AND ni.customerid = rp.customerid
JOIN nk_xjsg_process_leader nl ON nl.itemid = rp.id AND nl.customerid = rp.customerid
JOIN nk_user_info nui ON nui.id = nl.tid AND nui.customerid = nl.customerid
WHERE
cs.pid = '40289e8c87fff7f80188042c15d50010'
AND ni.flowtaskid IN ('40289e8b87a7c0ef0188527f09700206', '40289e8b87a7c0ef018852359f600204')
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)