There are two tables, in one entities are stored, and in the second they are versioned. How to choose the latest versions for these entities?
To get the output
| account_actual_id | account_id | name | added_date | |______________________________________________________| | 3 | 2 |'First' | 2016-11-03 | |______________________________________________________| | 4 | 5 |'Second'| 2016-11-03 | Structure for tests.
CREATE TABLE `account` ( `account_id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NULL DEFAULT NULL, PRIMARY KEY (`account_id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB; CREATE TABLE `account_actual` ( `account_actual_id` BIGINT(20) NOT NULL AUTO_INCREMENT, `account_id` INT(11) NOT NULL, `added_date` DATE NOT NULL, PRIMARY KEY (`account_actual_id`), UNIQUE INDEX `u` (`account_id`, `added_date`), INDEX `added_date` (`added_date`), INDEX `account_id` (`account_id`), CONSTRAINT `fk__account_actual_account` FOREIGN KEY (`account_id`) REFERENCES `account` (`account_id`) ON UPDATE CASCADE ON DELETE CASCADE ) COLLATE='utf8_general_ci' ENGINE=InnoDB; INSERT INTO `account` VALUES (2, 'First'); INSERT INTO `account` VALUES (3, 'Second'); INSERT INTO `account_actual` VALUES (1, 2, '2016-11-01'); INSERT INTO `account_actual` VALUES (2, 3, '2016-11-01'); INSERT INTO `account_actual` VALUES (3, 2, '2016-11-03'); INSERT INTO `account_actual` VALUES (4, 3, '2016-11-03'); INSERT INTO `account_actual` VALUES (5, 2, '2016-11-02'); INSERT INTO `account_actual` VALUES (6, 3, '2016-11-02'); http://sqlfiddle.com/#!9/933f9c/1
Productivity is extremely important. Million tables