以下有两个 SQL,请比较一下哪个 SQL 更好,为什么?
- 第一个 SQL
SELECT
a.userId,
a.level,
CASE a.level
WHEN 1 THEN count(c.userId) * 10
WHEN 2 THEN count(c.userId) * 12
WHEN 3 THEN count(c.userId) * 14
WHEN 4 THEN count(c.userId) * 16
END zdMoney
FROM bco_spread.sp_fat_salary_user a
LEFT JOIN bco_spread.sp_fat_salary_relation b ON a.userId = b.recommendUserId AND b.recommendStatus = 0 AND current_date BETWEEN b.yieldBeginTime AND date_add(b.yieldBeginTime, INTERVAL 150 DAY)
LEFT JOIN bco_spread.sp_fat_salary_user c ON b.recommendedUserId = c.userId AND c.status NOT IN (2, 3, 4)
WHERE a.status NOT IN (3, 4)
GROUP BY a.userId
- 第二个 SQL
SELECT
a.userId,
a.level,
CASE a.level
WHEN 1 THEN count(c.userId) * 10
WHEN 2 THEN count(c.userId) * 12
WHEN 3 THEN count(c.userId) * 14
WHEN 4 THEN count(c.userId) * 16
END zdMoney
FROM
bco_spread.sp_fat_salary_relation b
INNER JOIN bco_spread.sp_fat_salary_user c ON b.recommendedUserId = c.userId AND c.status NOT IN (2, 3, 4)
RIGHT JOIN bco_spread.sp_fat_salary_user a ON a.userId = b.recommendUserId AND b.recommendStatus = 0 AND current_date BETWEEN b.yieldBeginTime AND date_add(b.yieldBeginTime, INTERVAL 150 DAY)
WHERE a.status NOT IN (3, 4)
GROUP BY a.userId
PS:我会采纳最好的答案并公布在帖子中
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于