Advertisement
anshintsuu

Untitled

May 7th, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. I have this schema
  2. [ id | parent | inserted_on | data ],
  3. [ 1 | 0 | 2017-05-04 09:29:37 | include ],
  4. [ 2 | 1 | 2017-05-04 16:00:45 | include ],
  5. [ 3 | 0 | 2017-05-04 16:01:13 | include ],
  6. [ 4 | 1 | 2017-05-04 16:28:09 | include ],
  7. [ 5 | 0 | 2017-05-04 16:28:11 | include ],
  8. [ 6 | 1 | 2017-05-04 16:28:18 | include ],
  9. [ 7 | 5 | 2017-05-04 16:28:23 | include ],
  10. [ 8 | 1 | 2017-05-04 16:28:32 | exclude ],
  11. [ 9 | 3 | 2017-05-04 16:28:39 | include ],
  12. [ 10 | 0 | 2017-05-04 16:28:41 | include ]
  13.  
  14. And I need to make a virtual table which generate an `index` and `updated_on` column.
  15. 1. Select all data with parent = 0
  16. 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
  17. 3. Order the table by `updated_on` in descending order.
  18. 4. Index it
  19.  
  20. with this query:
  21. 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;
  22.  
  23. I expect this result:
  24. [ index | id | parent | timestamp | data | updated_on DESC ],
  25. [ 1 | 10 | 0 | 2017-05-04 16:28:41 | include | 2017-05-04 16:28:41 ],
  26. [ 2 | 3 | 0 | 2017-05-04 16:01:13 | include | 2017-05-04 16:28:39 ],
  27. [ 3 | 1 | 0 | 2017-05-04 09:29:37 | include | 2017-05-04 16:28:18 ],
  28. [ 4 | 5 | 0 | 2017-05-04 16:28:11 | include | 2017-05-04 16:28:23 ]
  29.  
  30. but instead, I get:
  31. [ index | id | parent | timestamp | data | updated_on DESC ],
  32. [ 4 | 10 | 0 | 2017-05-04 16:28:41 | include | 2017-05-04 16:28:41 ],
  33. [ 2 | 3 | 0 | 2017-05-04 16:01:13 | include | 2017-05-04 16:28:39 ],
  34. [ 3 | 1 | 0 | 2017-05-04 09:29:37 | include | 2017-05-04 16:28:18 ],
  35. [ 1 | 5 | 0 | 2017-05-04 16:28:11 | include | 2017-05-04 16:28:23 ]
  36.  
  37. Where did I get it wrong?
  38.  
  39. Schema query:
  40. CREATE TABLE `datas` (
  41. `id` int(11) NOT NULL,
  42. `parent` int(11) NOT NULL,
  43. `inserted_on` datetime NOT NULL,
  44. `data` varchar(16) COLLATE utf8_general_mysql500_ci NOT NULL
  45. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_mysql500_ci ROW_FORMAT=DYNAMIC;
  46. INSERT INTO `datas` (`id`, `parent`, `inserted_on`, `data`) VALUES
  47. (1, 0, '2017-05-04 09:29:37', 'include'),
  48. (2, 1, '2017-05-04 16:00:45', 'include'),
  49. (3, 0, '2017-05-04 16:01:13', 'include'),
  50. (4, 1, '2017-05-04 16:28:09', 'include'),
  51. (5, 0, '2017-05-04 16:28:11', 'include'),
  52. (6, 1, '2017-05-04 16:28:18', 'include'),
  53. (7, 5, '2017-05-04 16:28:23', 'include'),
  54. (8, 1, '2017-05-04 16:28:32', 'exclude'),
  55. (9, 3, '2017-05-04 16:28:39', 'include'),
  56. (10, 0, '2017-05-04 16:28:41', 'include');
  57.  
  58. [SOLVED]
  59. The solution is to change LEFT JOIN to INNER JOIN.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement