关于同时查询父子名称的SQL查询语句的写法 id name parentId parentName

本文介绍了一种SQL查询技巧,用于从包含id和parentId的表中同时查询出子公司的名称及其对应父公司的名称,通过联表查询实现,适用于JDBC方式或纯SQL语句查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

parentid是1就是id为1的公司的子公司
如图  查询出所有的信息后 由于我要呈现的是parentName 不是parentId所以想问下SQL语句怎么写 谢谢啦~~:)

 

 

解法:

SELECT sub.orgName AS 公司名, main.orgName AS 父公司名FROM 表名 main JOIN 表名 sub ON (main.id = sub.parentId)

 

在开发中近常会遇到这样的查询(如果是JDBC方式或者纯SQL语句查询的话):

如果一个表里面存在id和parentId,你又想同时查询出name和parentName既可以使用下面的方式查询哦。

select child.NAME,child.ID,child.PARENT_ID,parent.name as PARENT_NAME FROM tb_dictionary parent left join tb_dictionary child on parent.id=child.parent_id 

 亲自测试,查询结果如下:

如果你觉得本文帮助了你,请顶一下,谢谢

  •                     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">9</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/jpr1990">
                    <img src="https://profile.csdnimg.cn/E/0/8/3_jpr1990" class="avatar_pic" username="jpr1990">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/1x/11.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/jpr1990" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">Ai2015WER</a></span>
                                            </div>
                    <div class="text"><span>发布了330 篇原创文章</span> · <span>获赞 25</span> · <span>访问量 198万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=jpr1990" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
``` <?xml version="1.0" encoding="UTF-8"?> <!D0CTYPE mapper PuBLIc "-//mybatis.org//0To Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper,dtd"s <mapper namespace="cn.com.cnnp erdb.rmr.conm.psatype.mapper .PsaTypeMapper"> 通用查询映射结果--> <resultMap id="psaTpeResultMap" type="cn.com.cnnp.endb.rmr.conm.psatype. vo .PsaTypeV0"> <result column="id" property="id"/> <result column="code" property="code"/> <result column="name" property="name"/> <result column="parent_id" property="parentId"/> <result column="remark" property="remark"/> <result column="plant_id" property="plantId"/> <result column="unit_id” property="unitId"/> <result column="update_user" property="updateUser"/> <result column="update_time" property="updateTime"/> <result column="create_user" property="createUser"/> <result column="create_time" property="createTime"/> <result column="is_deleted" property="isDeleted"/> </resultMap> <select id="selectPsaTypePage" resultType="cn .com.cnnp.erdb.rmr.conm.psatype. vo.PsaTypeVo"> select a.id, a.code, a.name, a.parent id, a.plant id, a.unit id, a.remark, a.update user, a.update time, a.create user, a.create time, a.is deleted, ifnull(parent.code,")as parentCode, ifnull(parent.name,"')as parentName from rmr_cf_psa_type a left join rmr_cf_psa_type parent on a.parent_id = parent.id left join erm_dm.dm_eg_unit u on a.unit_id = u.id <where> a.is_deleted = 0 <if test="vo.plantId != null and vo.plantId != "> and a.plant_id =#{vo.plantId} </if> sif test="vo,unitId != null and vo.unitId !=!"> and a.unit id= #{vo.unitId} </1f> </where> ORDER BY a.'plant_id’,(u.*unit_no*+0),parent.code, a.code </select> <select id="spaqe resultType="cn.com,cnnp.epdb.rmn.conm.psatype.vo.PsaTypeselectVo": select a.id, a.code, a.name, a.parent id, a.plant id, a.unit id, a.remark, a.update user, a.update time, a.create user, a.create time, a.is deleted, parent.code as parentCode, parent.name as parentName from rmr_cf_psa_type a left join rmr_cf_psa_type parent on a.parent_id = parent.id <where> a.is_deleted= 0 <choose> <when test="parentId != null and parentId != '""> and a.parent_id = #{parentId} </when> <otherwise> and(a.parent_id is null or a.parent_id ='') </otherwise> </choose> <if test="plantId != null and plantId != "'"> and a.plant_id = #{plantId} </if> <if test="unitId != null and unitId !="'"> and a.unit_id = #{unitId} </if> </where> 0RDER BY a.plant_id,parent.code, a.code </select> <select id="spages" resultType="cn.com .cnnp.endb.rmr conm.psatype. vo .PsaTypeSelectVo"> select a.id, a.code, a.name, a.parent id, a.plant id, a.unit id, a.remark, a.update user, a.update time, a.create user, a.create time. a is deleted parent.code as parentCode, parent.name as parentName from rmr_cf_psa_type a left join rmr_cf_psa_type parent on a.parent_id = parent.id cwhere> a.is_deleted= 0 <choose> swhen test="parentId != null and parentId != '!"> and a.parent_id = #{parentId} <when test="parentId != null and parentId != ''"> and a.parent_id = #{parentId} </when> <otherwise> AND a.1d NOT IN (SELEcT parent_id FRoM rmr_cf_psa_type WHERE is_deleted = '@' AND parent_1d IS NOT NULL) </otherwise> </choose> <if test="plantId != null and plantId !=''"> and a.plant_id =#{plantId} </if> <if test="unitId != null and unitId != '&middot;“> and a.unit_id = #{unitId} </if> </where> ORDER BY a.`plant_id`,parent.code, a.code </select> <select id="stdDeviceType" resultType="cn.com.gnnp.ardb.rmr.gon.psatype.vo.stdDeviceTypelistVo"> select id, name , code, fid, level from erdb_device_type <where> is_deleted =0 <choose> <when test="level != null and level != '"> <choose> <when test="level =='',toString()"> and fid in(select id from erdb_device_type where fid = #{fid}) and level = 3 </when> <otherwise> and level = #{level} <if test="fid != null and fid != ""> and fid = #{fid} </if> </otherwise> </choose> </when> <otherwise> and level = 3 <if test="fid != null and fid != ''"> and fid = #{fid} </if> </otherwise> </choose> </where> </select> /mapper>```代码所实现的功能详情解析
最新发布
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值