I have two tables- "events" and "rooms".
The rooms table looks like this:
CREATE TABLE `rooms` (
`roomKey` INT(9) UNSIGNED NOT NULL AUTO_INCREMENT,
`locationID` INT(9) NOT NULL DEFAULT '0',
`locationName` VARCHAR(150) NOT NULL DEFAULT '',
`buildingID` INT(9) NOT NULL DEFAULT '0',
`buildingName` VARCHAR(150) NOT NULL DEFAULT '',
`areaID` INT(9) NOT NULL DEFAULT '0',
`areaName` VARCHAR(150) NOT NULL DEFAULT '',
`roomID` INT(9) NOT NULL DEFAULT '0',
`roomName` VARCHAR(150) NOT NULL DEFAULT '',
`clientKey` MEDIUMINT(5) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`roomKey`),
UNIQUE INDEX `locationID` (`locationID`, `buildingID`, `areaID`, `roomID`, `clientKey`),
INDEX `clientKey` (`clientKey`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=21270411;
and the events table looks like this:
CREATE TABLE `events` (
`eventKey` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`accountNum` INT(11) NULL DEFAULT NULL,
`locationID` INT(9) NOT NULL DEFAULT '0',
`buildingID` INT(9) NOT NULL DEFAULT '0',
`areaID` INT(9) NOT NULL DEFAULT '0',
`roomID` INT(9) NOT NULL DEFAULT '0',
`clientKey` SMALLINT(5) UNSIGNED NOT NULL,
`roomKey` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`eventTitle` VARCHAR(100) NOT NULL DEFAULT '',
`dateStart` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`dateEnd` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`status` SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`eventKey`),
UNIQUE INDEX `uniqueEvent` (`accountNum`, `locationID`, `buildingID`, `areaID`, `roomID`),
INDEX `clientKey` (`clientKey`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=98690419;
There is a process which inserts a bulk import of new events into the events table. Once it is complete, we need to update the new records with the roomKey that corresponds to the primary key in the "rooms" table - which we can match up using the locationID, buildingID, areaID, roomID and clientKey of a record in the rooms table. (clientKey is necessary as multiple accounts could have the same location, bldg, area, or room values).
Currently, the query I use for this is:
UPDATE events e, rooms dr SET e.locationKey=dr.locationKey
WHERE e.locationID=dr.locationID
AND e.buildingID=dr.buildingID
AND e.areaID=dr.areaID
AND e.roomID=dr.roomID
AND e.clientKey=dr.clientKey
But it does not take into consideration the instances when two accounts (clients) have the same values in location, bldg, area and room. The query is susceptible to putting the wrong roomKey value in the event record based on the fact that another account has a room record with the same locationID, buildingID, areaID and roomID.
I can modify the query to run like shown below, but it is extremely slow. I know it's not the right query to use, so I am looking to improve it - without having to do this update with a PHP loop, etc.
UPDATE EVENTS e, rooms dr SET e.locationKey=dr.locationKey
WHERE (e.locationID=dr.locationID AND e.clientKey=dr.clientKey)
AND (e.buildingID=dr.buildingID AND e.clientKey=dr.clientKey)
AND (e.areaID=dr.areaID AND e.clientKey=dr.clientKey)
AND (e.roomID=dr.roomID AND e.clientKey=dr.clientKey)
AND e.clientKey=dr.clientKey