tw4ld6 发表于 2024-8-4 09:57:39

MySQL数据库小技能:内外连接


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">理解SQL<span style="color: black;">查找</span>的过程是进行SQL优化的理论依据。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SQL<span style="color: black;">查找</span>的基本原理:</p>单表<span style="color: black;">查找</span>:<span style="color: black;">按照</span>WHERE<span style="color: black;">要求</span>过滤表中的记录,形成中间表(这个中间表对用户是不可见的);<span style="color: black;">而后</span><span style="color: black;">按照</span>SELECT的<span style="color: black;">选取</span>列<span style="color: black;">选取</span>相应的列进行返回<span style="color: black;">最后</span>结果。两表连接<span style="color: black;">查找</span>:对两表求积(笛卡尔积)并用ON<span style="color: black;">要求</span>和连接类型进行过滤形成中间表;<span style="color: black;">而后</span><span style="color: black;">按照</span>WHERE<span style="color: black;">要求</span>过滤中间表的记录,并<span style="color: black;">按照</span>SELECT指定的列返回<span style="color: black;">查找</span>结果。多表连接<span style="color: black;">查找</span>:先对<span style="color: black;">第1</span>个和第二个表<span style="color: black;">根据</span>两表连接做<span style="color: black;">查找</span>,<span style="color: black;">而后</span>用<span style="color: black;">查找</span>结果和第三个表做连接<span style="color: black;">查找</span>,以此类推,直到所有的表都连接上为止,<span style="color: black;">最后</span>形成一个中间的结果表,<span style="color: black;">而后</span><span style="color: black;">按照</span>WHERE<span style="color: black;">要求</span>过滤中间表的记录,并<span style="color: black;">按照</span>SELECT指定的列返回<span style="color: black;">查找</span>结果。<h1 style="color: black; text-align: left; margin-bottom: 10px;">I 内连接 (inner join)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在一个表中能够找到在另一个表中与之对应的记录,来<span style="color: black;">构成</span>新的记录<span style="color: black;">表示</span>出来,舍弃表中在另一个表中<span style="color: black;">无</span>对应记录的记录。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">inner join (inner<span style="color: black;">能够</span>省略): 查出的是两张表的交集,两张表都有的才查出来</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select * from 表A inner join 表B on 判断<span style="color: black;">要求</span>;</p><span style="color: black;">select</span> r.*,m.merchant_name,m.facilitator_id,m.facilitator_name,m.facilitator_top_id,m.facilitator_top_name <span style="color: black;">from</span> risk_equipment_position r <span style="color: black;">inner</span> <span style="color: black;">join</span> mer_merchant m <span style="color: black;">on</span> r.merchant_id = m.merchant_id <span style="color: black;">ORDER</span> <span style="color: black;">BY</span> create_time <span style="color: black;">DESC</span> <span style="color: black;">LIMIT</span> <span style="color: black;">15</span> ;

    复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">1.1 等值连接</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一个表中的某个字段是另一个表的外键。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如: emp表和dept表存在多对一的<span style="color: black;">相关</span>关系,由外键字段deptno来<span style="color: black;">守护</span>,即emp表来主动<span style="color: black;">守护</span>这一关系。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.2 非等值连接</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">between and 来实现非等值连接;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select e.ename,e.sal,s.grade from emp e join salgrades on e.sal between s.losal and s.hisal;</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.3 自连接( nature join)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">表中的字段之间存在引用关系,<span style="color: black;">经过</span>同一个表取<span style="color: black;">区别</span>别名来实现<span style="color: black;">自己</span>连接。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select e.empno,m.name from emp e join emp m on m.empno=e.mgr;</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">II 外连接</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外连接,将驱动表中所有记录<span style="color: black;">表示</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">说明:</p>左表<span style="color: black;">便是</span>在“(LEFT OUTER JOIN)”关键字左边的表。右表是右边的。在三种类型的外连接中,OUTER 关键字是可省略的。<h1 style="color: black; text-align: left; margin-bottom: 10px;">2.1 左外连接 (left join)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">左外连接会将主表对应的所有信息<span style="color: black;">表示</span>,从表与之匹配的记录<span style="color: black;">表示</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以左表为主表(<span style="color: black;">查找</span><span style="color: black;">所有</span>), 右表为辅表(<span style="color: black;">无</span>的<span style="color: black;">表示</span>null)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">案例1:</p><span style="color: black;">select</span> * <span style="color: black;">from</span> emp <span style="color: black;">join</span>dept<span style="color: black;">on</span> emp.deptno=dept.deptno(+); <span style="color: black;">--加号在join 右边的表的属性上为左连接</span>

    <span style="color: black;">select</span> * <span style="color: black;">from</span> emp <span style="color: black;">left</span> <span style="color: black;">join</span> dept <span style="color: black;">on</span> emp.deptno=dept.deptno;


    复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">案例2: <span style="color: black;">查找</span>商户终端定位限制</p><span style="color: black;">select</span>r.*,m.merchant_name,m.facilitator_id,m.facilitator_name,m.facilitator_top_id,m.facilitator_top_name<span style="color: black;">from</span> risk_equipment_position r <span style="color: black;">left</span> <span style="color: black;">join</span> mer_merchant m <span style="color: black;">on</span>r.merchant_id = m.merchant_id<span style="color: black;">ORDER</span> <span style="color: black;">BY</span> create_time <span style="color: black;">DESC</span> <span style="color: black;">LIMIT</span> <span style="color: black;">15</span> ;

    复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">2.2 右外连接 (right join)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">加号在join 左边的表的属性上为右连接</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以右表为主表(<span style="color: black;">查找</span><span style="color: black;">所有</span>记录), 左表为辅表(<span style="color: black;">无</span>的<span style="color: black;">表示</span>null)</p><span style="color: black;">select</span>r.* ,m.merchant_name,m.facilitator_id,m.facilitator_name,m.facilitator_top_id,m.facilitator_top_name<span style="color: black;">from</span> risk_equipment_position r <span style="color: black;">right</span> <span style="color: black;">join</span> mer_merchant m <span style="color: black;">on</span>r.merchant_id = m.merchant_id<span style="color: black;">ORDER</span> <span style="color: black;">BY</span> create_time <span style="color: black;">DESC</span> <span style="color: black;">LIMIT</span> <span style="color: black;">15</span>
    复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">2.3 全连接 ( full join)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将两表中所有匹配和不匹配记录<span style="color: black;">表示</span>出来:两个表的所有数据都展示出来</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MySQL 不识别 FULL join,<span style="color: black;">因此</span><span style="color: black;">能够</span><span style="color: black;">经过</span> union 来实现。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2.4 联合(合并)查询(union)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">案例1:</p><span style="color: black;">select</span> a.*, ad.* <span style="color: black;">from</span> test_a <span style="color: black;">as</span> a <span style="color: black;">left</span> <span style="color: black;">join</span> test_a_description <span style="color: black;">as</span> ad <span style="color: black;">on</span> a.id=ad.parent_id
    <span style="color: black;">union</span>
    <span style="color: black;">select</span> a.*, ad.* <span style="color: black;">from</span> test_a <span style="color: black;">as</span> a <span style="color: black;">right</span> <span style="color: black;">join</span> test_a_description <span style="color: black;">as</span> ad <span style="color: black;">on</span> a.id=ad.parent_id;
    复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">案例2:</p><span style="color: black;">select</span> r.*,m.merchant_name,m.facilitator_id,m.facilitator_name,m.facilitator_top_id,m.facilitator_top_name <span style="color: black;">from</span>risk_equipment_position r<span style="color: black;">left</span> <span style="color: black;">join</span> mer_merchant m <span style="color: black;">on</span> r.merchant_id = m.merchant_id <span style="color: black;">union</span> <span style="color: black;">select</span>r.*,m.merchant_name,m.facilitator_id,m.facilitator_name,m.facilitator_top_id,m.facilitator_top_name<span style="color: black;">from</span> t_risk_equipment_position r <span style="color: black;">right</span> <span style="color: black;">join</span> t_mer_merchant m <span style="color: black;">on</span> r.merchant_id = m.merchant_id <span style="color: black;">ORDER</span> <span style="color: black;">BY</span> create_time <span style="color: black;">DESC</span> <span style="color: black;">LIMIT</span> <span style="color: black;">15</span>;
    复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">III 案例:mybatis-plus 多表<span style="color: black;">查找</span></h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.1 多表<span style="color: black;">查找</span>:在mapper.xml 写语句和拼接<span style="color: black;">查找</span><span style="color: black;">要求</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在mapper.xml 写语句,对应<span style="color: black;">办法</span>写到mapper里,service<span style="color: black;">能够</span>注入对应的mapper进行调用。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.xml</p> &lt;<span style="color: black;">select</span> id=<span style="color: black;">"selectProfitTradeActivityList"</span>
    resultType=<span style="color: black;">"com.dto.TradeActivityDto"</span>
    parameterType=<span style="color: black;">"com.dto.GenerateTradeQuery"</span>&gt;
    <span style="color: black;">select</span> a.id,a.trade_no,a.activity_type,a.activity_code
    <span style="color: black;">from</span> trans_flow t
    inner <span style="color: black;">join</span> trans_flow_activity a <span style="color: black;">on</span> t.trade_no = a.trade_no
    &lt;<span style="color: black;">where</span>&gt;
    t.trade_state=<span style="color: black;">S</span> and a.pay_channel_id=<span style="color: black;">XYD</span>
    &lt;<span style="color: black;">if</span> test=<span style="color: black;">"ps.startTradeTime != null"</span>&gt;
    and t.trade_time &gt;= <span style="color: black;">#{ps.startTradeTime}</span>
    &lt;/<span style="color: black;">if</span>&gt;

    &lt;/<span style="color: black;">where</span>&gt;
    -- order <span style="color: black;">by</span> t.update_time desc
    &lt;/<span style="color: black;">select</span>&gt;
    复制代码<span style="color: black;">@Mapper</span>
    <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">interface</span> <span style="color: black;">TransFlowActivityMapper</span> <span style="color: black;">extends</span> <span style="color: black;">BaseMapper</span>&lt;<span style="color: black;">TransFlowActivity</span>&gt; </span>{

    <span style="color: black;">List&lt;GenerateTradeActivityDto&gt; <span style="color: black;">selectProfitTradeActivityList</span><span style="color: black;">(@Param(<span style="color: black;">"ps"</span>)</span>GenerateTradeQuery input)</span>;

    }

    复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">3.2 多表<span style="color: black;">相关</span>:Java代码中书写语句和拼接<span style="color: black;">查找</span><span style="color: black;">要求</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现思路:</p><span style="color: black;">运用</span> Select注解书写sql语句采用apply<span style="color: black;">办法</span>自定义sql拼接<span style="color: black;">要求</span>, <span style="color: black;">运用</span><span style="color: black;">要求</span>构造器<span style="color: black;">做为</span>参数。驱动表表和辅表的参数拼接书写SQL<h1 style="color: black; text-align: left; margin-bottom: 10px;">3.3 案例:左外连接</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现思路:</p><span style="color: black;">运用</span> Select注解书写sql语句/xml书写语句。采用apply<span style="color: black;">办法</span>自定义sql拼接<span style="color: black;">要求</span>, <span style="color: black;">运用</span>wrapper<span style="color: black;">要求</span>构造器<span style="color: black;">做为</span>参数。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现<span style="color: black;">过程</span>:</p>定义<span style="color: black;">查找</span>接口: mapper.java/Service.javaService层<span style="color: black;">@Override</span>
    <span style="color: black;"><span style="color: black;">public</span>List&lt;RiskEquipmentPositionDto&gt;<span style="color: black;">getlist</span><span style="color: black;">(LambdaQueryWrapper&lt;RiskEquipmentPosition&gt; input)</span> </span>{
    <span style="color: black;">return</span>baseMapper.getList(input);
    }
    复制代码Mapper层:采用 ${ew.customSqlSegment}和@Param(Constants.WRAPPER)实现,将<span style="color: black;">要求</span>构造器<span style="color: black;">做为</span>参数。书写SQL 方式一:<span style="color: black;">运用</span>Select注解书写sql语句<span style="color: black;">@Select</span>(<span style="color: black;">"select r.* ,m.merchant_name,m.facilitator_id,m.facilitator_name,m.facilitator_top_id,m.facilitator_top_name from risk_equipment_position r left join mer_merchant m on r.merchant_id = m.merchant_id ${ew.customSqlSegment} "</span>)
    List&lt;RiskEquipmentPositionDto&gt; getList(<span style="color: black;">@Param</span>(Constants.WRAPPER) LambdaQueryWrapper&lt;RiskEquipmentPosition&gt; input);
    复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">where后面的内容<span style="color: black;">便是</span><span style="color: black;">要求</span>构造器生成的部分</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式2: <span style="color: black;">运用</span>xml书写语句</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/2082043a87bc4c3eb670782c5dadbf63~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723338874&amp;x-signature=2Jf39MMw9B2mKa%2BUk%2BmcsLIKpiM%3D" style="width: 50%; margin-bottom: 20px;"></div>拼接<span style="color: black;">查找</span><span style="color: black;">要求</span>LambdaQueryWrapper&lt;RiskEquipmentPosition&gt; lambda = <span style="color: black;">new</span> LambdaQueryWrapper&lt;&gt;();
    lambda.orderByDesc(TRiskEquipmentPosition::getCreateTime);
    <span style="color: black;">if</span> (input.getStartTime() != <span style="color: black;">null</span>) {
    lambda.ge(RiskEquipmentPosition::getCreateTime, input.getStartTime());
    }
    <span style="color: black;">if</span> (input.getEndTime() != <span style="color: black;">null</span>) {
    lambda.lt(RiskEquipmentPosition::getCreateTime, input.getEndTime());
    }<span style="color: black;">if</span> (input.getState() != <span style="color: black;">null</span>) {
    lambda.eq(RiskEquipmentPosition::getState, input.getState());
    }<span style="color: black;">// 终端SN、商户号本表<span style="color: black;">查找</span>,其他关键词<span style="color: black;">必须</span><span style="color: black;">相关</span>商户表进行<span style="color: black;">查找</span>。</span>
    <span style="color: black;">if</span> (<span style="color: black;">null</span> != input.getKeywordType() &amp;&amp; StringUtils.isNotBlank(input.getKeyword())) {

    <span style="color: black;">var</span>keyWordTypes =<span style="color: black;">new</span>ArrayList&lt;String&gt;();
    keyWordTypes.add(ERiskEquipmentPositionKeyWord.MER_ID.getCode());
    keyWordTypes.add(ERiskEquipmentPositionKeyWord.FAC_ID.getCode());
    keyWordTypes.add(ERiskEquipmentPositionKeyWord.TOP_ID.getCode());
    Long longKeyWord =<span style="color: black;">0L</span>;
    <span style="color: black;">if</span>(keyWordTypes.contains(input.getKeywordType())) {<span style="color: black;">try</span> {
    longKeyWord = Long.parseLong(input.getKeyword());
    } <span style="color: black;">catch</span> (Exception ex) {
    <span style="color: black;">throw</span>CommonException.create(ServerResponse.createByError(<span style="color: black;">"无效的编号"</span>));
    }
    }
    <span style="color: black;">switch</span> (BaseEnum.getEnum(ERiskEquipmentPositionKeyWord<span style="color: black;">.<span style="color: black;">class</span>, <span style="color: black;">input</span>.<span style="color: black;">getKeywordType</span>())) </span>{
    <span style="color: black;">case</span>SN:
    lambda.eq(RiskEquipmentPosition::getEquipmentSn, input.getKeyword());<span style="color: black;">break</span>;
    <span style="color: black;">case</span> MER_ID:
    lambda.apply(<span style="color: black;">"r.merchant_id = {0}"</span>,input.getKeyword());
    <span style="color: black;">break</span>;
    <span style="color: black;">case</span>MER_NAME:
    lambda.apply(<span style="color: black;">"m.merchant_name = {0}"</span>,input.getKeyword());
    <span style="color: black;">break</span>;
    <span style="color: black;">case</span> FAC_ID:
    <span style="color: black;">//apply<span style="color: black;">办法</span>用于拼接自定义的<span style="color: black;">要求</span>判断,<span style="color: black;">倘若</span>自定义的<span style="color: black;">要求</span>判断是<span style="color: black;">必须</span>独立的结果,就<span style="color: black;">必要</span>记得加括号。</span>
    lambda.apply(<span style="color: black;">"m.facilitator_id = {0}"</span>,input.getKeyword());
    <span style="color: black;">break</span>;
    <span style="color: black;">case</span> FAC_NAME:
    lambda.apply(<span style="color: black;">"m.facilitator_name = {0}"</span>,input.getKeyword());
    <span style="color: black;">break</span>;
    <span style="color: black;">case</span> TOP_ID:
    lambda.apply(<span style="color: black;">"m.faclitator_top_id= {0}"</span>,input.getKeyword());
    <span style="color: black;">break</span>;
    <span style="color: black;">case</span> TOP_NAME:
    lambda.apply(<span style="color: black;">"m.facilitator_top_name= {0}"</span>,input.getKeyword());
    <span style="color: black;">break</span>;
    }

    }
    <span style="color: black;">// 分页<span style="color: black;">查找</span></span>PageHelper.startPage(input.getPageNum(), input.getPageSize());
    List&lt;RiskEquipmentPositionDto&gt; positionList = tRiskEquipmentPositionService.getlist(lambda);
    PageInfo pageInfo =<span style="color: black;">new</span> PageInfo(positionList);
    PageHelper.clearPage();
    <span style="color: black;">return</span> pageInfo;
    复制代码调用<span style="color: black;">查找</span>接口 List<span style="color: black;">&lt;<span style="color: black;">RiskEquipmentPositionDto</span>&gt;</span> positionList = RiskEquipmentPositionService.getlist(lambda);

    复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">IV 笛卡尔积 (cross join)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">交差集: 把表A和表B的数据进行一个N*M的组合,即笛卡尔积。 SELECT * FROM TableA CROSS JOIN TableB</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">V 小结:</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">5.1 外连接的区别分析</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外连接:外连<span style="color: black;">不仅</span>返回符合连接和<span style="color: black;">查找</span><span style="color: black;">要求</span>的数据行,还返回不符合<span style="color: black;">要求</span>的<span style="color: black;">有些</span>行。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外连接分三类:左外连接(LEFT OUTER JOIN)、右外连接(RIGHT OUTER JOIN)和全外连接(FULL OUTER JOIN)</p>三者的<span style="color: black;">一起</span>点是都返回符合连接<span style="color: black;">要求</span>和<span style="color: black;">查找</span><span style="color: black;">要求</span>(即:内连接)的数据行。<span style="color: black;">区别</span>点如下:左外连接:还返回左表中不符合连接<span style="color: black;">要求</span>但符合<span style="color: black;">查找</span><span style="color: black;">要求</span>的数据行。右外连接:还返回右表中不符合连接<span style="color: black;">要求</span>但符合<span style="color: black;">查找</span><span style="color: black;">要求</span>的数据行。全外连接:还返回左表中不符合连接<span style="color: black;">要求</span>但符合<span style="color: black;">查找</span><span style="color: black;">要求</span>的数据行,并且还返回右表中不符合连接<span style="color: black;">要求</span>但符合<span style="color: black;">查找</span><span style="color: black;">要求</span>的数据行。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">全外连接<span style="color: black;">实质</span>是上左外连接和右外连接的数学合集(去掉重复),即“全外 = 左外 UNION 右外”。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">5.2 Column merchant_id in where clause is ambiguous</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">错误代码:SELECT count(0) FROM risk_equipment_position r LEFT JOIN mer_merchant m ON r.merchant_id = m.merchant_id WHERE (merchant_id = ?)</p>case MER_ID:
    lambda.e<span style="color: black;">q(TRiskEquipmentPosition::getMerchantId, input.getKeyword()</span>);
    <span style="color: black;">break</span>;
    复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">原由</span>:多表<span style="color: black;">查找</span>的时候几个表中<span style="color: black;">同期</span><span style="color: black;">显现</span>了某个相同的列名,而在<span style="color: black;">查找</span><span style="color: black;">要求</span>WHERE后面又<span style="color: black;">无</span>指定是那个表,而<span style="color: black;">导致</span>的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">处理</span><span style="color: black;">方法</span>:where <span style="color: black;">要求</span>语句后<span style="color: black;">查找</span>某个字段要加上表名。<span style="color: black;">能够</span><span style="color: black;">运用</span>mybatis的apply<span style="color: black;">办法</span>来拼接自定义的<span style="color: black;">要求</span>判断。</p> <span style="color: black;">case</span>MER_ID:
    lambda.apply(<span style="color: black;">"r.merchant_id = {0}"</span>,input.getKeyword());
    <span style="color: black;">break</span>;
    <span style="color: black;">case</span> MER_NAME:
    lambda.apply(<span style="color: black;">"m.merchant_name = {0}"</span>,input.getKeyword());
    <span style="color: black;">break</span>;
    <span style="color: black;">case</span> FAC_ID:
    <span style="color: black;">//apply<span style="color: black;">办法</span>用于拼接自定义的<span style="color: black;">要求</span>判断,<span style="color: black;">倘若</span>自定义的<span style="color: black;">要求</span>判断是<span style="color: black;">必须</span>独立的结果,就<span style="color: black;">必要</span>记得加括号。</span>
    lambda.apply(<span style="color: black;">"m.facilitator_id = {0}"</span>,input.getKeyword());
    <span style="color: black;">break</span>;
    复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">关联</span>问题:<span style="color: black;">查找</span>结果里面有两个相同的列名,而<span style="color: black;">无</span>指定是哪个表<span style="color: black;">运用</span>的时候,sql<span style="color: black;">查找</span>前面加表名可避免<span style="color: black;">显现</span>错误。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">VI 集合</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">集合<span style="color: black;">查找</span>中的字段个数和类型要一致,这<span style="color: black;">叫作</span>为select的同构。</p><span style="color: black;">select</span> job <span style="color: black;">from</span> emp deptno=<span style="color: black;">10</span> <span style="color: black;">union</span> <span style="color: black;">all</span> <span style="color: black;">select</span> job <span style="color: black;">from</span> emp deptno=<span style="color: black;">20</span>;

    复制代码union all 并集,并且不去除重复union 并集,去除重复intersect 交集minus 差集<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">原文链接:</p>https://juejin.cn/post/7183296217126797369
    <span style="color: black;">源自</span>:稀土掘金




dhfgh 发表于 2024-9-3 18:00:09

谷歌外贸网站优化技术。
页: [1]
查看完整版本: MySQL数据库小技能:内外连接