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

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值