我的 SQL - 获取特定搜索的值,然后添加并最终放入相同 table 的另一列
My SQL - getting values for specific search and then adding and finally putting in another column of same table
我想将执行某个查询后得到的某个搜索的值相加,最后将它们分别放在同一个table的不同列中。
select TotalAmount from Payment_Data where CreatedAt like "%04-07%", insert into VendorPIAmount;
就像我在执行顶级查询后得到了值,但我不知道如何添加它们并最终将它们放在另一列中。
借助一些现有的库,很容易实现这个功能,但是如果你的数据量比较大,fastexcel是一个非常好的apache的替代品poi.Reference:https://github.com/dhatim/fastexcel/。另外,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");
}
}
依赖项:
<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>
代码中使用的table是根据您的描述在本地mariadb数据库中创建的。 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');
我想将执行某个查询后得到的某个搜索的值相加,最后将它们分别放在同一个table的不同列中。
select TotalAmount from Payment_Data where CreatedAt like "%04-07%", insert into VendorPIAmount;
就像我在执行顶级查询后得到了值,但我不知道如何添加它们并最终将它们放在另一列中。
借助一些现有的库,很容易实现这个功能,但是如果你的数据量比较大,fastexcel是一个非常好的apache的替代品poi.Reference:https://github.com/dhatim/fastexcel/。另外,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");
}
}
依赖项:
<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>
代码中使用的table是根据您的描述在本地mariadb数据库中创建的。 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');