Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. DROP FUNCTION IF EXISTS SPLIT_STR;
  2.  
  3. CREATE FUNCTION SPLIT_STR(
  4. x VARCHAR(255),
  5. delim VARCHAR(12),
  6. pos INT
  7. )
  8. RETURNS VARCHAR(255)
  9. RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
  10. LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
  11. delim, '');
  12.  
  13. DELIMITER //
  14.  
  15. DROP PROCEDURE IF EXISTS auth_categs//
  16.  
  17. create procedure auth_categs(IN to_id INT(10), IN from_list VARCHAR(200))
  18. begin
  19. DECLARE commercial_category_id BIGINT(20);
  20. DECLARE from_list BIGINT(20);
  21. DECLARE cat_exists BIGINT(20);
  22. DECLARE user_elem_id BIGINT(20);
  23. DECLARE commercial_category_lists VARCHAR(200);
  24.  
  25. # get user_elem and a mysql set with commercial_categs ex: 17,45,32
  26. DECLARE list_ids CURSOR FOR
  27. SELECT ac.user_elem_id, GROUP_CONCAT(ac.commercial_category_id SEPARATOR ',') as commercial_category_lists
  28. FROM authored_categories as ac
  29. WHERE FIND_IN_SET(ac.commercial_category_id, from_list)
  30. Group by ac.user_elem_id;
  31.  
  32. #parse the results
  33. OPEN list_ids;
  34. lists_loop: LOOP
  35. FETCH list_ids INTO user_elem_id, commercial_category_lists;
  36. IF FIND_IN_SET(to_id, commercial_category_list)=0 THEN
  37. # if not in set update the first one
  38. UPDATE authored_categories SET commercial_category_id=to_id WHERE commercial_category_id=SPLIT_STR(commercial_category_list, ',', 1) and user_elem_id=user_elem_id;
  39. END IF;
  40. DELETE from authored_categories WHERE commercial_category_id in (from_list) and user_elem_id=user_elem_id;
  41.  
  42. END LOOP lists_loop;
  43. end
  44. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement