Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I have this schema
- [ id | parent | inserted_on | data ],
- [ 1 | 0 | 2017-05-04 09:29:37 | include ],
- [ 2 | 1 | 2017-05-04 16:00:45 | include ],
- [ 3 | 0 | 2017-05-04 16:01:13 | include ],
- [ 4 | 1 | 2017-05-04 16:28:09 | include ],
- [ 5 | 0 | 2017-05-04 16:28:11 | include ],
- [ 6 | 1 | 2017-05-04 16:28:18 | include ],
- [ 7 | 5 | 2017-05-04 16:28:23 | include ],
- [ 8 | 1 | 2017-05-04 16:28:32 | exclude ],
- [ 9 | 3 | 2017-05-04 16:28:39 | include ],
- [ 10 | 0 | 2017-05-04 16:28:41 | include ]
- And I need to make a virtual table which generate an `index` and `updated_on` column.
- 1. Select all data with parent = 0
- 2. Generate `updated_on` by this logic: `updated_on` = count children > 0 ? last children's inserted_on : inserted_on of itself; and then join it
- 3. Order the table by `updated_on` in descending order.
- 4. Index it
- with this query:
- SELECT @i := @i + 1 AS `index`, `p`.`id`, `p`.`parent`, `p`.`inserted_on`, `t`.`updated_on` FROM `datas` AS `p` JOIN (SELECT @i := 0) AS `iterator` LEFT JOIN (SELECT IF(`parent` = 0, `id`, `parent`) AS `timestamp_of`, MAX(`inserted_on`) AS `updated_on` FROM `datas` WHERE `data` != 'exclude' GROUP BY `timestamp_of`) AS `t` ON `p`.`id` = `t`.`timestamp_of` WHERE `p`.`parent` = '0' ORDER BY `t`.`updated_on` DESC;
- I expect this result:
- [ index | id | parent | timestamp | data | updated_on DESC ],
- [ 1 | 10 | 0 | 2017-05-04 16:28:41 | include | 2017-05-04 16:28:41 ],
- [ 2 | 3 | 0 | 2017-05-04 16:01:13 | include | 2017-05-04 16:28:39 ],
- [ 3 | 1 | 0 | 2017-05-04 09:29:37 | include | 2017-05-04 16:28:18 ],
- [ 4 | 5 | 0 | 2017-05-04 16:28:11 | include | 2017-05-04 16:28:23 ]
- but instead, I get:
- [ index | id | parent | timestamp | data | updated_on DESC ],
- [ 4 | 10 | 0 | 2017-05-04 16:28:41 | include | 2017-05-04 16:28:41 ],
- [ 2 | 3 | 0 | 2017-05-04 16:01:13 | include | 2017-05-04 16:28:39 ],
- [ 3 | 1 | 0 | 2017-05-04 09:29:37 | include | 2017-05-04 16:28:18 ],
- [ 1 | 5 | 0 | 2017-05-04 16:28:11 | include | 2017-05-04 16:28:23 ]
- Where did I get it wrong?
- Schema query:
- CREATE TABLE `datas` (
- `id` int(11) NOT NULL,
- `parent` int(11) NOT NULL,
- `inserted_on` datetime NOT NULL,
- `data` varchar(16) COLLATE utf8_general_mysql500_ci NOT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_mysql500_ci ROW_FORMAT=DYNAMIC;
- INSERT INTO `datas` (`id`, `parent`, `inserted_on`, `data`) VALUES
- (1, 0, '2017-05-04 09:29:37', 'include'),
- (2, 1, '2017-05-04 16:00:45', 'include'),
- (3, 0, '2017-05-04 16:01:13', 'include'),
- (4, 1, '2017-05-04 16:28:09', 'include'),
- (5, 0, '2017-05-04 16:28:11', 'include'),
- (6, 1, '2017-05-04 16:28:18', 'include'),
- (7, 5, '2017-05-04 16:28:23', 'include'),
- (8, 1, '2017-05-04 16:28:32', 'exclude'),
- (9, 3, '2017-05-04 16:28:39', 'include'),
- (10, 0, '2017-05-04 16:28:41', 'include');
- [SOLVED]
- The solution is to change LEFT JOIN to INNER JOIN.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement