使用select查询加载数据infile进行数据查找

时间:2013-10-08 00:25:58

标签: mysql

我能够使用以下create和load data infile命令成功加载文件:

--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...行的正确方法是什么(如果有办法)?

2 个答案:

答案 0 :(得分:1)

您可以设置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;

我希望能帮助你。

答案 1 :(得分: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);