Advertisement
vallec

Untitled

Apr 3rd, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. /* 1 */
  2. use school_sport_clubs;
  3. DELIMITER $
  4. DROP PROCEDURE IF EXISTS GetCoachDetails;
  5. CREATE PROCEDURE GetCoachDetails(IN coach_name VARCHAR(255))
  6. BEGIN
  7. SELECT
  8. sg.location AS training_location,
  9. sg.dayOfWeek AS training_day,
  10. sg.hourOfTraining AS training_hour,
  11. s.name AS sport_name,
  12. st.name AS student_name,
  13. st.phone AS student_phone
  14. FROM
  15. coaches c
  16. JOIN
  17. sportGroups sg ON c.id = sg.coach_id
  18. JOIN
  19. sports s ON sg.sport_id = s.id
  20. JOIN
  21. student_sport ss ON sg.id = ss.sportGroup_id
  22. JOIN
  23. students st ON ss.student_id = st.id
  24. WHERE
  25. c.name = coach_name;
  26. END $
  27.  
  28. DELIMITER ;
  29.  
  30. CALL GetCoachDetails('Ivan Todorov Petkov');
  31.  
  32. /* 2 */
  33.  
  34. DELIMITER $
  35. DROP PROCEDURE IF EXISTS GetSportDetails;
  36. CREATE PROCEDURE GetSportDetails(IN sport_id INT)
  37. BEGIN
  38. SELECT
  39. s.name AS sport_name,
  40. st.name AS student_name,
  41. c.name AS coach_name
  42. FROM
  43. sports s
  44. JOIN
  45. sportGroups sg ON s.id = sg.sport_id
  46. JOIN
  47. coaches c ON sg.coach_id = c.id
  48. JOIN
  49. student_sport ss ON sg.id = ss.sportGroup_id
  50. JOIN
  51. students st ON ss.student_id = st.id
  52. WHERE
  53. s.id = sport_id;
  54. END $
  55.  
  56. DELIMITER ;
  57.  
  58. CALL GetSportDetails(1);
  59.  
  60. /* 3 */
  61. DELIMITER $
  62. DROP PROCEDURE IF EXISTS GetAveragePaymentsByStudent;
  63. CREATE PROCEDURE GetAveragePaymentsByStudent(IN student_name VARCHAR(255), IN payment_year YEAR)
  64. BEGIN
  65. SELECT
  66. AVG(tp.paymentAmount) AS average_payment
  67. FROM
  68. students as st
  69. JOIN
  70. taxesPayments as tp ON st.id = tp.student_id
  71. WHERE
  72. st.name = student_name
  73. AND YEAR(tp.dateOfPayment) = payment_year;
  74. END $
  75.  
  76. DELIMITER ;
  77.  
  78. CALL GetAveragePaymentsByStudent('Iliyan Ivanov', 2024);
  79.  
  80. /* 4 */
  81. DELIMITER $
  82.  
  83. CREATE PROCEDURE GetCoachGroupsCount(IN coach_name VARCHAR(255))
  84. BEGIN
  85. DECLARE groups_count INT;
  86.  
  87. SELECT COUNT(*) INTO groups_count
  88. FROM sportGroups sg
  89. JOIN coaches c ON sg.coach_id = c.id
  90. WHERE c.name = coach_name;
  91.  
  92. IF groups_count > 0 THEN
  93. SELECT groups_count AS coach_groups_count;
  94. ELSE
  95. SELECT 'Този треньор не води никакви групи.' AS message;
  96. END IF;
  97. END $
  98.  
  99. DELIMITER ;
  100.  
  101. CALL GetCoachGroupsCount('Ivan Todorov Petkov');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement