实践一:

    1. select
    2. *
    3. from
    4. agent_info ai
    5. inner join
    6. (
    7. select
    8. acr.relation_id agentId,
    9. acr.city_id,
    10. dp.`name` provinceName,
    11. dc.`name` cityName,
    12. concat(dp.`name`,'/',dc.`name`) cityProvinceName
    13. from
    14. agent_city_relation acr
    15. left join
    16. dic_city dc
    17. on
    18. acr.city_id = dc.id
    19. left join
    20. dic_province dp
    21. on
    22. dc.province_id = dp.id
    23. where
    24. acr.city_id in
    25. ( 15)
    26. and
    27. acr.type = 1
    28. ) t1
    29. on
    30. ai.agent_id = t1.agentId
    31. where
    32. ai.agent_level =1

    用时:
    image.png
    (1)超过三个表禁止join

    如果需要超过三个表的,我们可以进行拆分,拆分的规则是如果查询的字段是再第三张表中那么我们就在这次SQL语句写,如果没有则在下一个SQL中写。