sql left join之后 类似like操作的效果
问题背景
- 表1 优惠券表 ticket
id | product_id |
---|---|
1 | 1 |
- 表2 商品信息表 product
id | product_id | product_detail |
---|---|---|
1 | 2019-1 | 1234 |
2 | 2018-1 | 1234 |
- 优惠券表里面有商品ID字段,需要去商品信息表里面通过product_id字段获取商品信息,商品信息表的product_id字段比ticket表多一个前缀,现在需要left join 查询商品信息。
环境
- mysql 5.7
效果
- 从固定位置开始截取到最后
select ticket.id, product.product_id,product.product_detail
from ticket
LEFT JOIN product on ticket.product_id = SUBSTRING(product.id,6,LENGTH(product.id))
输出结果
id | product_id | product_detail |
---|---|---|
1 | 2019-1 | 1234 |
1 | 2018-1 | 1234 |