Mysql+SQL server 两张表通过关联条件更新列数据

当需要做令人恶心的数据修复的任务事情时;

常常需要使用以下语句,先把原始数据表备份出来。

create table 表名_bak as (select * from 表名);

但当你的修复方案失败的时候,往往需要先把数据先还原回去。

平时使用两张表关联查询较多,更新动作较少。

以下语句个人觉得比较好用,并且逻辑清晰,增加字段也不用重写。

Mysql:

update 表名1  a  join 表名2 b  on  a.xx=b.xx  set a.xx1=b.xx1,a.xx2=b.xx2;

Sql server:

update 表名1 
    set 表名1.xx=表名2.xx 
from 表名1 left join 表名2 
    on 表名1.关联字段1=表名2.关联字段2;

 Oracle:

update 表名1 
    set 表名1.xx=(select 表名2.xx from 表名2 where 表名1.关联字段1=表名2.关联字段2)
where exists (select 1 from 表名2 where 表名1.关联字段1=表名2.关联字段2);

可以使用Mybatis-Plus提供的多数据源配置,然后在查询时指定使用哪个数据源。以下是示例代码: 1. 配置多数据源 在application.yml文件中配置两个数据源: ```yaml spring: datasource: master: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: root slave: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: root ``` 在配置类中添加多数据源配置: ```java @Configuration @MapperScan(basePackages = "com.example.mapper") public class DataSourceConfig { @Bean("master") @ConfigurationProperties("spring.datasource.master") public DataSource masterDataSource() { return DataSourceBuilder.create().build(); } @Bean("slave") @ConfigurationProperties("spring.datasource.slave") public DataSource slaveDataSource() { return DataSourceBuilder.create().build(); } @Bean public SqlSessionFactory sqlSessionFactory(@Qualifier("master") DataSource masterDataSource, @Qualifier("slave") DataSource slaveDataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(routingDataSource(masterDataSource, slaveDataSource)); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath*:mapper/**/*.xml")); return sessionFactory.getObject(); } @Bean public DataSource routingDataSource(@Qualifier("master") DataSource masterDataSource, @Qualifier("slave") DataSource slaveDataSource) { Map<Object, Object> dataSourceMap = new HashMap<>(); dataSourceMap.put("master", masterDataSource); dataSourceMap.put("slave", slaveDataSource); RoutingDataSource routingDataSource = new RoutingDataSource(); routingDataSource.setDefaultTargetDataSource(masterDataSource); routingDataSource.setTargetDataSources(dataSourceMap); return routingDataSource; } } ``` 2. 实现两个关联查询 在mapper中编写SQL语句,可以使用Mybatis-Plus提供的Wrapper类来实现多条件查询。 ```java @Mapper public interface UserMapper extends BaseMapper<User> { @Select("SELECT * FROM user u JOIN order o ON u.id = o.user_id WHERE u.id = #{userId}") @Results({ @Result(column = "id", property = "id"), @Result(column = "username", property = "username"), @Result(column = "id", property = "orders", many = @Many(select = "com.example.mapper.OrderMapper.selectByUserId")), }) User selectUserWithOrders(@Param("userId") Long userId); } @Mapper public interface OrderMapper extends BaseMapper<Order> { @Select("SELECT * FROM order WHERE user_id = #{userId}") List<Order> selectByUserId(Long userId); } ``` 在service中调用mapper方法即可实现两个关联查询。 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserWithOrders(Long userId) { return userMapper.selectUserWithOrders(userId); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天亦可蓝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值