Join vs. sub-query

前端 未结 19 2104
广开言路
广开言路 2020-11-21 05:05

I am an old-school MySQL user and have always preferred JOIN over sub-query. But nowadays everyone uses sub-query, and I hate it; I don\'t know why.

19条回答
  •  自闭症患者
    2020-11-21 05:18

    If you want to speed up your query using join:

    For "inner join/join", Don't use where condition instead use it in "ON" condition. Eg:

         select id,name from table1 a  
       join table2 b on a.name=b.name
       where id='123'
    
     Try,
    
        select id,name from table1 a  
       join table2 b on a.name=b.name and a.id='123'
    

    For "Left/Right Join", Don't use in "ON" condition, Because if you use left/right join it will get all rows for any one table.So, No use of using it in "On". So, Try to use "Where" condition

提交回复
热议问题