我能够使用以下创建和加载数据文件命令成功加载文件:
--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...
(如果有办法)?