Guest User

Untitled

a guest
Apr 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. drop table tree;
  2.  
  3. create table tree (
  4. id INT,
  5. path VARCHAR(255),
  6. name VARCHAR(100)
  7. );
  8.  
  9. insert into tree VALUES (1, '/', 'Prédio Orozimbo');
  10. insert into tree VALUES (2, '/1', 'Ala a');
  11. insert into tree VALUES (3, '/1', 'Ala b');
  12. insert into tree VALUES (4, '/1/2', 'RH');
  13. insert into tree VALUES (5, '/1/2', 'ADM');
  14. insert into tree VALUES (6, '/1/3', 'Tecnico');
  15. insert into tree VALUES (7, '/1/3', 'Visitante');
  16. insert into tree VALUES (8, '/1/3/7', 'sala b');
  17. insert into tree VALUES (9, '/1/3/7/8', 'sala d');
  18. insert into tree VALUES (10, '/1/2/5', 'sala a');
  19. insert into tree VALUES (11, '/1/2/5', 'sala c');
  20.  
  21. // Exemplo pesquisa por nome
  22. SELECT DISTINCT parent.id, parent.path, parent.name
  23. FROM tree AS leaf
  24. JOIN tree AS parent
  25. WHERE leaf.name LIKE '%sala%'
  26. AND ((leaf.name = parent.name AND leaf.path = parent.path)
  27. OR (leaf.path LIKE CONCAT('%', parent.id, '%')));
  28.  
  29. // Exemplo pesquisa por nome e tags
  30. SELECT DISTINCT parent.id, parent.path, parent.name
  31. FROM tree AS leaf
  32. JOIN tree AS parent
  33. WHERE (leaf.name LIKE '%visita%') AND (leaf.tags LIKE '%emuso%' OR leaf.tags LIKE '%virtual%')
  34. AND ((leaf.name = parent.name AND leaf.path = parent.path)
  35. OR (leaf.path LIKE CONCAT('%', parent.id, '%')));
Add Comment
Please, Sign In to add comment