SQL将一个表的数据写入到另一个表中

转载自:https://www.cnblogs.com/xu-yi/p/10623493.html

方法一:

写存储过程

方法二:

Insert Into Order_1  (OrderNo,Name,CreateOn,Creater,...)

Select OrderNo,Name,CreateOn,Creater,... FROM Order_2 Where type=1;

Order_1后面的字段列表一定要与Order_2查询的字段列表的顺序一一对应。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Flink的JDBC连接器来读取MySQL数据,并使用Flink SQL数据进行分析和转换,然后再使用JDBC连接器将结果写入另一个MySQL。 以下是一个简单的示例代码: ```java //创建Flink环境 StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env); //创建MySQL连接器 JdbcConnectionOptions connectionOptions = JdbcConnectionOptions.builder() .withUrl("jdbc:mysql://localhost:3306/mydatabase") .withDriverName("com.mysql.jdbc.Driver") .withUsername("myuser") .withPassword("mypassword") .build(); JdbcCatalog catalog = new JdbcCatalog("mycatalog", connectionOptions); tableEnv.registerCatalog("mycatalog", catalog); //读取MySQL数据 String sourceTable = "mytable"; String sql = "SELECT * FROM " + sourceTable; Table source = tableEnv.sqlQuery(sql); //SQL分析并转换 String resultTable = "result"; sql = "SELECT col1, col2, col3 FROM " + sourceTable + " WHERE col1 > 10"; Table result = tableEnv.sqlQuery(sql); //写入MySQL JdbcOutputFormat jdbcOutputFormat = JdbcOutputFormat.buildJdbcOutputFormat() .setDBUrl("jdbc:mysql://localhost:3306/mydatabase") .setDrivername("com.mysql.jdbc.Driver") .setUsername("myuser") .setPassword("mypassword") .setQuery("INSERT INTO myresulttable (col1, col2, col3) VALUES (?, ?, ?)") .finish(); DataStream<Row> dataStream = tableEnv.toAppendStream(result, Row.class); dataStream.addSink(new JdbcOutputSinkFunction(jdbcOutputFormat)); //执行任务 env.execute("MySQL to MySQL"); ``` 需要注意的是,在写入MySQL时,需要使用JdbcOutputFormat和JdbcOutputSinkFunction来实现。其,JdbcOutputFormat需要设置数据库连接信息和写入语句,JdbcOutputSinkFunction将Table转换为DataStream后,再使用JdbcOutputFormat将数据写入MySQL

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值