• 【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等等条件语句。

  • 相关阅读:
    I Hate It
    hdu 2112 HDU Today ( Dijkstra )
    hdu 1280 前m大的数
    hdu 4252 A Famous City
    hdu 2647 Reward
    hdu 2845 Beans
    hdu 3548 Enumerate the Triangles ( 优 化 )
    hdu 3552 I can do it! (贪心)
    HDU 3033 I love sneakers!(分组背包变形)
    hdu 1712 ACboy needs your help 分组背包
  • 原文地址:https://www.cnblogs.com/haibaowang/p/6758034.html
Copyright © 2020-2023  润新知