gpt4 book ai didi

mysql - 使用选择查询加载数据文件以进行数据查找

转载 作者:行者123 更新时间:2023-11-29 13:32:22 24 4
gpt4 key购买 nike

我能够使用以下创建和加载数据文件命令成功加载文件:

--Create and load addresses
CREATE TABLE `addresses` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`address1` varchar(100) NOT NULL,
`address2` varchar(100) DEFAULT NULL,
`city` varchar(100) NOT NULL,
`stateCode` varchar(2) NOT NULL,
`zipCode` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8;

LOAD DATA INFILE 'C:/temp/address.dat'
INTO TABLE mbc.addresses
FIELDS
TERMINATED BY X'1F'
LINES
TERMINATED BY X'1E'
(name,address1,address2,city,stateCode,zipCode);

现在,我想使用加载到地址中的地址 ID 将数据从另一个文件 (receipt.dat) 加载到收据表中。目前,地址名称存储在receipt.dat 的“addressId”字段中。我想做这样的事情,但不确定正确的语法是什么:

--Create and load receipts
CREATE TABLE `receipts` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`receiptDate` date NOT NULL,
`addressId` bigint(20) NOT NULL,
`amount` decimal(10,0) NOT NULL,
`notes` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `addressFK_idx` (`addressId`),
CONSTRAINT `addressFK` FOREIGN KEY (`addressId`) REFERENCES `addresses` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

LOAD DATA INFILE 'C:/temp/receipt.dat'
INTO TABLE mbc.receipts
FIELDS
TERMINATED BY X'1F'
LINES
TERMINATED BY X'1E'
(receiptDate,
select max(addressId) from mbc.addresses where name = @addressId,
amount,
notes);

格式化上面的 select max(addressId... 行(如果有的话)的正确方法是什么?

最佳答案

您可以设置addresId:

--Create and load receipts
CREATE TABLE `receipts` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`receiptDate` date NOT NULL,
`addressId` bigint(20) NOT NULL,
`amount` decimal(10,0) NOT NULL,
`notes` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `addressFK_idx` (`addressId`),
CONSTRAINT `addressFK` FOREIGN KEY (`addressId`) REFERENCES `addresses` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

LOAD DATA INFILE 'C:/temp/receipt.dat'
INTO TABLE mbc.receipts
FIELDS
TERMINATED BY X'1F'
LINES
TERMINATED BY X'1E'
(receiptDate, amount, notes)
SET addressId = select max(addressId) from mbc.addresses where name = @addressId;

希望对你有帮助。

关于mysql - 使用选择查询加载数据文件以进行数据查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19237105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com