sql关联查询+表自关联查询
Posted Angel挤一挤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql关联查询+表自关联查询相关的知识,希望对你有一定的参考价值。
表:
经销商 dealer 字段 uid parent_uid name
联系人 contact 字段 uid dealer_id contact_main
需求:
想要查询到经销商的信息,和本经销商的主要联系人的信息 ,和本经销商的父级经销商的name
情况:
可能有parent_id是null
sql语句:
SELECT dea.uid uid, dea.enabled_flag enabledFlag, dea.delete_flag deleteFlag, dea.tenement_id tenementId, dea.parent_id parentId, pd.name parentName, dea.name name, dea.type type, dea.bar_code barCode, dea.outer_code outerCode, dea.outer_id outerId, dea.mne_code mneCode, dea.address address, dea.address_xy addressXy, dea.business_area businessArea, dea.business_area_xy businessAreaXy, con.name contactName, con.mobile mobile FROM dealer AS dea LEFT JOIN (select * from contact where tenement_id = ‘9f63f84f-52c6-4c8e-b3c3-66b9f1f283ba‘ and main_contact = 1) con ON dea.uid = con.dealer_id LEFT JOIN dealer pd on pd.uid = dea.parent_id
以上是关于sql关联查询+表自关联查询的主要内容,如果未能解决你的问题,请参考以下文章