简体   繁体   English

我的 SQL - 获取特定搜索的值,然后添加并最后放入同一张表的另一列

[英]My SQL - getting values for specific search and then adding and finally putting in another column of same table

I want to add values of a certain search that I got after executing a certain query and finally put them in a different column of the same table separately.我想添加执行某个查询后获得的某个搜索的值,最后将它们分别放在同一个表的不同列中。

select TotalAmount from Payment_Data where CreatedAt like "%04-07%", insert into VendorPIAmount; select TotalAmount from Payment_Data where CreatedAt like "%04-07%", 插入 VendorPIAmount;

like I got values after executing the top query but I don't know how to add them and finally put them in another column.就像我在执行顶级查询后得到了值,但我不知道如何添加它们,最后将它们放在另一列中。

With the help of some existing libraries and easy to implement this function, but if your data volume is relatively large, fastexcel is a very good replacement for apache poi.Reference: https://github.com/dhatim/fastexcel/ .In addition, hutool is a collection of great tools, which can easily operate the database.借助一些现有的库,并且很容易实现这个function,但是如果你的数据量比较大,fastexcel是apache poi的一个很好的替代品。参考: Z5E056C500A1C4B6A7110A1C4B6A7110A1C4B6A710A1C4B6A7110.com另外/dtimexcel。 , hutool 是一个很棒的工具集合,可以很方便的操作数据库。 It also has many other commonly used functions, date format conversion, string processing, etc. Reference: https://github.com/dromara/hutool它还有很多其他常用功能,日期格式转换,字符串处理等。参考: https://github.com/dromara/hutool

package com.example.demo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.DbUtil;
import cn.hutool.db.ds.simple.SimpleDataSource;
import org.dhatim.fastexcel.Color;
import org.dhatim.fastexcel.Workbook;
import org.dhatim.fastexcel.Worksheet;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.OutputStream;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

public class DataExportTest {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * export data to excel
     */
    @Test
    public void Test2() {
        LocalDateTime start = LocalDateTime.now();
        try (OutputStream os = FileUtil.getOutputStream("D:\\tmp\\TEST_DATA_2_EXCEL.xlsx")) {
            SimpleDataSource ds = new SimpleDataSource("jdbc:mariadb://localhost:3306/litemall", "root", "rooter", "org.mariadb.jdbc.Driver");
            List<String> list = DbUtil.use(ds).query("SELECT TotalAmount  from Payment_Data where CreatedAt like ?", String.class, "%04-07%");
            Workbook wb = new Workbook(os, "MyApplication", "1.0");
            Worksheet ws = wb.newWorksheet("Sheet 1");
            ws.value(0, 0, "VendorPIAmount");
            ws.style(0, 0).fillColor(Color.GRAY2).set();
            for (int i = 0; i < list.size(); i++) {
                ws.value(i + 1, 0, list.get(i));
            }
            wb.finish();
        } catch (Exception e) {
            logger.error("Error", e);
        }
        LocalDateTime end = LocalDateTime.now();
        logger.info("Cost time {}", Duration.between(start, end).toMillis() + "ms");
    }
}

Dependencys:依赖性:

<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <version>2.7.4</version>
</dependency>
<dependency>
    <groupId>org.dhatim</groupId>
    <artifactId>fastexcel-reader</artifactId>
    <version>0.12.12</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.16</version>
</dependency>

The table used in the code was created in the local mariadb database according to your description.代码中使用的表是根据您的描述在本地 mariadb 数据库中创建的。 The DDL is as follows. DDL 如下。

-- ----------------------------
-- Table structure for payment_data
-- ----------------------------
DROP TABLE IF EXISTS `payment_data`;
CREATE TABLE `payment_data`  (
  `ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `CreatedAt` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
  `TotalAmount` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of payment_data
-- ----------------------------
INSERT INTO `payment_data` VALUES (1, 'DCBA01-17ABCD', 'AAA');
INSERT INTO `payment_data` VALUES (2, 'DEDCB02-32BCDE', 'BBB');
INSERT INTO `payment_data` VALUES (3, 'ASDF04-07FDSA', 'CCC');
INSERT INTO `payment_data` VALUES (4, 'ZXCV04-07VCXZ', 'DDD');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM