MySQL Data Table Traversal method

Source: Internet
Author: User

MySQL traversal of data tables is a common problem. The following describes how to traverse data tables in MySQL. I hope it will help you learn how to traverse data tables in MySQL.

 
 
  1. DELIMITER $
  2.  
  3. USE 'tns $
  4.  
  5. Drop procedure if exists 'makefriends '$
  6.  
  7. Create definer = 'root' @ 'localhost' PROCEDURE 'makefriends '()
  8. BEGIN
  9. DECLARE v_adminId BIGINT (12 );
  10. DECLARE v_currentUserID BIGINT (12 );
  11. DECLARE v_ExistRela INT;
  12. DECLARE Done int default 0;
  13. DECLARE rs cursor for select userId FROM hw_user;
  14. /* Exception Handling */
  15. Declare continue handler for sqlstate '000000' SET Done = 1;
  16.  
  17. SET v_adminId = 1;
  18.  
  19. /* Open the cursor */
  20. OPEN rs;
  21.  
  22. /* Retrieve the values of The userId field of the current record one by one */
  23. Fetch next from rs INTO v_currentUserID;
  24. /* Traverse the data table */
  25. REPEAT
  26. If not Done THEN
  27. Select count (*) FROM hw_relationship WHERE sourceId = v_currentUserID AND targetId = v_adminId INTO v_ExistRela;
  28. /* SELECT v_currentUserID; SELECT v_ExistRela ;*/
  29. IF v_currentUserID! = V_adminId AND v_ExistRela = 0 THEN
  30. /* Insert relation records */
  31. Insert into hw_relationship (createTime, relationType, sourceId, targetId) VALUES (NOW (), 1, v_currentUserID, v_adminId );
  32. Insert into hw_relationship (createTime, relationType, sourceId, targetId) VALUES (NOW (), 1, v_adminId, v_currentUserID );
  33. End if;
  34. End if;
  35.  
  36. Fetch next from rs INTO v_currentUserID;
  37.  
  38. UNTIL Done end repeat;
  39.  
  40. /* Close the cursor */
  41. CLOSE rs;
  42. END $
  43.  
  44. DELIMITER;
  45.  

Implementation of batch data import in MySQL

Create a temporary table in php mysql

Quick table creation in mysql

In-depth study on MySQL's data deletion from multiple tables

Simple MySQL large table backup method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.