zoukankan      html  css  js  c++  java
  • 【SQL提数】左连接使用

    1.表table_score结构如下:用来存储分数。

    表table_no结构如下:用来存储相关合同号、签约号、保单号。

    表table_base结构如下:用来存储基础数据:

    我们现在想要取到三个表中以table_base为基础,与table_score,table_no关联的数据。

    2.书写思路如下

    (1)首先明确三个表中都有相同的字段apply_id、business_No

    (2)其中business_NO与apply_id是一一对应的。

    (3)其中table_base表中的一个apply_id对应表table_score中有多条记录。

    3.写法如下

    select a.apply_id,

        a.business_no,

              b.apply_id,

              b.app_no,

              b.policy_no,

    (select score from table_score c where a.apply_id=c.apply_id and c.score_resource='01'),

    (select score from table_score d where a.apply_id=d.apply_id and d.score_resource='02'),

     from   table_base a

    left join table_no b on a.apply_id=b.apply_id;

    说明如下:上述描述的写法需要注意以下问题:

    (1)(select score from table_score c where a.apply_id=c.apply_id and c.score_resource='01')这里查询出来的也是一列元素;

    (2)左连接左边的表必须只有一个,我们写的语句中是table_base;

    (3)多个表中必须有一个相同的字段将三张表进行关联,否则无法实现多表查询的功能。

    (4)写法上注意规律性:多个表一定运用别名。

    select 后面就是写查询的字段

    from后面就是跟的表名

    如果有条件那么就用where和and等等条件语句。

  • 相关阅读:
    希尔排序
    基数排序
    spring8——AOP之Bean的自动代理生成器
    spring7——AOP之通知和顾问
    spring6——AOP的编程术语
    spring5——Aop的实现原理(动态代理)
    spring4——IOC之基于注解的依赖注入(DI )
    spring3——IOC之基于XML的依赖注入(DI )
    spring2——IOC之Bean的装配
    spring1——IOC之原理
  • 原文地址:https://www.cnblogs.com/haibaowang/p/6758034.html
Copyright © 2011-2022 走看看