2015-04-03 108 views
0

如何簡化此MySQL子查詢?簡化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')) 

回答

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' 
+0

作品!非常感謝Brian。 – 2015-04-03 02:34:02

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' 

謝謝!