SELECT
(CASE
WHEN
(SELECT COUNT(*)
FROM EMPLOYEE
WHERE PERCOD = DB.PERID) > 1 THEN
(SELECT PERNAME
FROM EMPLOYEE
WHERE PERCOD = DB.PERID
AND ISDELETED = 0)
ELSE
(SELECT PERNAME
FROM EMPLOYEE
WHERE PERCOD = DB.PERID)
END) AS PERNAME
FROM BALANCE DB
LEFT JOIN BALANCE_BONUS DBB
ON DBB.ID = DB.BONUSID
WHERE DB.ISDELETED != 1
ORDER BY DB.ID;
----------------------------------------------------(CASE
WHEN
(SELECT COUNT(*)
FROM EMPLOYEE
WHERE PERCOD = DB.PERID) > 1 THEN
(SELECT PERNAME
FROM EMPLOYEE
WHERE PERCOD = DB.PERID
AND ISDELETED = 0)
ELSE
(SELECT PERNAME
FROM EMPLOYEE
WHERE PERCOD = DB.PERID)
END) AS PERNAME
这一段代码会非常消耗查询时间。 作用是 排除 EMPLOYEE 表中PERCOD重复的数据 , ISDELETED字段为已删除。 首先查询级联的人员是否有重复数据,如果只有一条则显示这唯一的一条; 如果有多条,则显示ISDELETED字段为0的一条数据。 有没有大牛给个优化解决方法!!!