您的位置:首页技术文章
文章详情页

mysql case when 查询一个效率问题

【字号: 日期:2022-06-19 14:43:25浏览:18作者:猪猪

问题描述

第一种:

SELECT `user`.username, `user`.email, `user`.id AS user_id, user_class.level_id, user_class.class_id AS cid, user_class.create_time AS bontime, class.graduation_time, class.offline_graduation_time, class.is_baidan, class. NAME AS class, class.period_id, class.offline_period_id, company. NAME AS branch, company.id AS bidFROM `user_class`INNER JOIN `user` ON user_class.user_id = `user`.idINNER JOIN class ON user_class.class_id = class.idLEFT JOIN company ON class.company_id = company.idLEFT JOIN period ON class.period_id = period.idWHERE class. DISABLE = 1AND class.is_baidan IN (’1’, ’2’)AND `user_class`.level_id IN (’10’, ’12’, ’14’)AND CASEWHEN is_baidan = 1 THEN class.graduation_time=0 OR class.graduation_time > 1469980800WHEN is_baidan = 2 THEN class.offline_graduation_time = 0 OR class.offline_graduation_time > 1469980800END;

第二种:

SELECT `user`.username, `user`.email, `user`.id AS user_id, user_class.level_id, user_class.class_id AS cid, user_class.create_time AS bontime, class.graduation_time AS eontime, class.offline_graduation_time, class.is_baidan, class. NAME AS class, class.period_id, class.offline_period_id, company. NAME AS branch, company.id AS bidFROM `user_class`INNER JOIN `user` ON user_class.user_id = `user`.idINNER JOIN class ON user_class.class_id = class.idLEFT JOIN company ON class.company_id = company.idLEFT JOIN period ON class.period_id = period.idWHERE class. DISABLE = 1AND `user_class`.level_id IN (’10’, ’12’, ’14’)AND ( (class.is_baidan = 1 AND (class.graduation_time = 0 OR class.graduation_time > 1469980800) ) OR (class.is_baidan = 2 AND (class.offline_graduation_time=0 OR class.offline_graduation_time >1469980800) ));

这两个sql语句查询结果相同,想问问到底是哪个好一点

问题解答

回答1:

看mysql的执行计划,你这个sql语句不执行执行计划看,我觉得谁也告知不了你准确的答案,因为他们没有非常明显的性能差异。

相关文章: