简体   繁体   中英

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;

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. It also has many other commonly used functions, date format conversion, string processing, etc. Reference: 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. The DDL is as follows.

-- ----------------------------
-- 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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