• left join时筛选条件对查询结果的


    -- 创建表
    CREATE TABLE `table1`
    (
        `id`          int(11) NOT NULL AUTO_INCREMENT,
        `card_num`    varchar(60) DEFAULT NULL,
        `customer_id` varchar(60) DEFAULT NULL,
        PRIMARY KEY (`id`)
    ) ENGINE = InnoDB
      AUTO_INCREMENT = 12
      DEFAULT CHARSET = utf8mb4 COMMENT ='测试表1';
    
    -- 创建表
    CREATE TABLE `table2`
    (
        `id`          int(11)     NOT NULL AUTO_INCREMENT,
        `customer_id` varchar(60) DEFAULT NULL,
        `type`        varchar(60) DEFAULT NULL COMMENT '联系方式',
        `phone`       varchar(60) NOT NULL COMMENT '联系号码',
        PRIMARY KEY (`id`)
    ) ENGINE = InnoDB
      AUTO_INCREMENT = 12
      DEFAULT CHARSET = utf8mb4 COMMENT ='测试表2';
    
    -- 插入数据
    insert into table1
    values (1, 6223123456781001, 1001)
    insert into table1
    values (2, 6223123456781002, 1002)
    insert into table1
    values (3, 6223123456781003, 1003)
    
    -- 插入数据
    insert into table2
    values (1, 1001, 1, 010 - 78586)
    insert into table2
    values (2, 1001, 2, 18810123456)
    insert into table2
    values (3, 1002, 1, 010 - 837433)
    insert into table2
    values (4, 1003, 1, 010 - 837433)
    
    -- 筛选条件在最外层
    select t1.card_num, t2.phone, t2.type
    from table1 t1
             left join ssm.table2 t2 on t1.customer_id = t2.customer_id
    where t2.type = '2'
    
    -- 筛序条件在内层
    select t1.card_num, t2.phone,t2.type
    from table1 t1
             left join (select * from table2
    where type = '2') t2 on t1.customer_id = t2.customer_id
    

    创建的表格:

    table1

    table2

    结果:

  • 相关阅读:
    无界共创,明道云参与广汽本田IT新技术展
    2022年10个最佳地理空间数据分析 GIS 软件
    linux常用命令
    倾斜实景三维中的“我的世界“
    安装VMware
    基于html5+javascript技术开发的房贷利率计算器
    3、TypeScript高级数据类型
    Redis-分布式锁
    el-form 初值和resetFields问题
    java学习——异常处理机制
  • 原文地址:https://blog.csdn.net/benniaofei18/article/details/133901398