0

I was able to load in a file successfully using the following create and load data infile commands:

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

Now, I would like to load data from another file (receipt.dat) into a receipts table using the address id that was loaded into addresses. Currently, the address name is stored in the "addressId" field in receipt.dat. I'm looking to do something like this but not sure what the right syntax is:

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

What is the proper way to format the select max(addressId... line above (if there is a way)?

2 Answers 2

1

You can set the 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;

I hope help you.

0
0

This is what ended up working for me...hope this helps someone else

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.