Advertisement
cahyadsn

rekreasi query hierarki

Dec 14th, 2017
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.78 KB | None | 0 0
  1. --
  2. DROP TABLE IF EXISTS member;
  3. CREATE TABLE member(
  4.     id INT AUTO_INCREMENT PRIMARY KEY,
  5.     member_id VARCHAR(50)
  6. );
  7.  
  8. INSERT INTO member(member_id)
  9. VALUES
  10. ('1'),
  11. ('1.2'),
  12. ('1.2.5'),
  13. ('1.2.5.11'),
  14. ('1.2.5.12'),
  15. ('1.2.6'),
  16. ('1.3'),
  17. ('1.3.7'),
  18. ('1.3.7.13'),
  19. ('1.3.7.13.15'),
  20. ('1.3.7.14'),
  21. ('1.4'),
  22. ('1.4.8'),
  23. ('1.4.9'),
  24. ('1.4.10');
  25.  
  26. -- show all data :
  27.  
  28. SELECT *
  29. FROM member;
  30. +----+-------------+
  31. | id | member_id   |
  32. +----+-------------+
  33. |  1 | 1           |
  34. |  2 | 1.2         |
  35. |  3 | 1.2.5       |
  36. |  4 | 1.2.5.11    |
  37. |  5 | 1.2.5.12    |
  38. |  6 | 1.2.6       |
  39. |  7 | 1.3         |
  40. |  8 | 1.3.7       |
  41. |  9 | 1.3.7.13    |
  42. | 10 | 1.3.7.13.15 |
  43. | 11 | 1.3.7.14    |
  44. | 12 | 1.4         |
  45. | 13 | 1.4.8       |
  46. | 14 | 1.4.9       |
  47. | 15 | 1.4.10      |
  48. +----+-------------+
  49. 15 rows in set (0.00 sec)
  50.  
  51. -- show data that corresponding with node number 2:
  52.  
  53. SELECT *
  54. FROM member
  55. WHERE '2' REGEXP CONCAT('^(',REPLACE(member_id,'.','|'),')$') ;
  56. +----+-----------+
  57. | id | member_id |
  58. +----+-----------+
  59. |  2 | 1.2       |
  60. |  3 | 1.2.5     |
  61. |  4 | 1.2.5.11  |
  62. |  5 | 1.2.5.12  |
  63. |  6 | 1.2.6     |
  64. +----+-----------+
  65. 5 rows in set (0.00 sec)
  66.  
  67. -- show data that child of node number 2:
  68.  
  69. SELECT *
  70. FROM member
  71. WHERE '2' REGEXP CONCAT('^(',REPLACE(member_id,'.','|'),')$')
  72. AND LENGTH(member_id) > INSTR(member_id,'2');
  73. +----+-----------+
  74. | id | member_id |
  75. +----+-----------+
  76. |  3 | 1.2.5     |
  77. |  4 | 1.2.5.11  |
  78. |  5 | 1.2.5.12  |
  79. |  6 | 1.2.6     |
  80. +----+-----------+
  81. 4 rows in set (0.00 sec)
  82.  
  83. SELECT *
  84. FROM member
  85. WHERE '2' REGEXP CONCAT('^(',REPLACE(member_id,'.','|'),')$')
  86. AND LENGTH(member_id) = INSTR(member_id,'2');
  87. +----+-----------+
  88. | id | member_id |
  89. +----+-----------+
  90. |  2 | 1.2       |
  91. +----+-----------+
  92. 1 row in set (0.00 sec)
  93.  
  94. SELECT SUBSTRING_INDEX(member_id,'.',-1) AS data
  95. FROM member
  96. WHERE '2' REGEXP CONCAT('^(',REPLACE(member_id,'.','|'),')$')
  97. AND LENGTH(member_id) = INSTR(member_id,'2');
  98. +------+
  99. | data |
  100. +------+
  101. | 2    |
  102. +------+
  103. 1 row in set (0.00 sec)
  104.  
  105. SELECT SUBSTRING_INDEX(member_id,'.2',1) AS data
  106. FROM member
  107. WHERE '2' REGEXP CONCAT('^(',REPLACE(member_id,'.','|'),')$')
  108. AND LENGTH(member_id) = INSTR(member_id,'2');
  109. +------+
  110. | data |
  111. +------+
  112. | 1    |
  113. +------+
  114. 1 row in set (0.00 sec)
  115.  
  116. SELECT SUBSTRING_INDEX(member_id,'.5',1) AS data
  117. FROM member
  118. WHERE '5' REGEXP CONCAT('^(',REPLACE(member_id,'.','|'),')$')
  119. AND LENGTH(member_id) = INSTR(member_id,'5');
  120. +------+
  121. | data |
  122. +------+
  123. | 1.2  |
  124. +------+
  125. 1 row in set (0.00 sec)
  126.  
  127. SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(member_id,'.5',1),'.',-1) AS data
  128. FROM member
  129. WHERE '5' REGEXP CONCAT('^(',REPLACE(member_id,'.','|'),')$')
  130. AND LENGTH(member_id) = INSTR(member_id,'5');
  131.  
  132. +------+
  133. | data |
  134. +------+
  135. | 2    |
  136. +------+
  137. 1 row in set (0.00 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement