0

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

--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...(如果有办法)?

4

2 回答 2

1

您可以设置地址Id:

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

我希望能帮助你。

于 2013-10-08T01:01:20.783 回答
0

这就是最终为我工作的东西......希望这对其他人有帮助

--Create and load receipts (uses data from the table create/load scripts above)
CREATE TABLE `receipts` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `receiptDate` date NOT NULL,
  `addressId` bigint(20) NOT NULL,
  `designationId` bigint(20) NOT NULL,
  `amount` decimal(10,0) NOT NULL,
  `notes` varchar(1000) DEFAULT NULL,
  PRIMARY KEY (`id`)
) 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'
    (@var1,@var2,@var3,amount,notes)
    SET receiptDate = STR_TO_DATE(@var1,'%m/%d/%Y'),
    addressId = (select max(id) from addresses where name = @var2),
    designationId = (select max(id) from designations where name = @var3);
于 2013-10-09T01:51:25.113 回答