简化MySQL子查询

时间:2015-04-03 00:54:51

标签: mysql subquery simplify

如何简化此MySQL子查询?

Select * From tbl_sys_usuarios Where usr_CvePA IN 
(Select sub_usrCveDep From tbl_sys_usuarios_subalternos Where sub_usrCvePA IN (Select sub_usrCveDep From tbl_sys_usuarios_subalternos Where sub_usrCvePA='237'))

2 个答案:

答案 0 :(得分:0)

这就是答案!我想是的......

Select * From tbl_sys_usuarios u
Join tbl_sys_usuarios_subalternos sub1 ON u.usr_CvePA=sub1.sub_UsrCveDep
Join tbl_sys_usuarios_subalternos sub2 ON sub1.sub_UsrCvePA=sub2.sub_UsrCveDep and sub2.sub_UsrCvePA='237'

谢谢!

答案 1 :(得分:0)

如果你试图摆脱嵌套查询,这应该有效:

SELECT tsu.*
FROM tbl_sys_usarios tsu
INNER JOIN tbl_sys_usarios_subalternos tsus_a ON tsu.usr_CvePA = tsus.sub_usrCveDep
INNER JOIN tbl_sys.tbl_sys_usarios_subalternos tsus_b ON tsus_a.sub_usrCvePA = tsus_b.sub_usrCveDep
WHERE tsus_b.sub_usrCvePA = '237'