Guest User

Untitled

a guest
Aug 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. trigger update another table's counter
  2. DROP TRIGGER IF EXISTS trigger_votes;
  3.  
  4. CREATE TRIGGER trigger_votes AFTER INSERT ON votes
  5. UPDATE collections
  6. SET IF(vote = 1, loves, hates) = IF(vote = 1, loves, hates) + 1
  7. WHERE collections.id = NEW.collection_id;
  8.  
  9. DELIMITER $$
  10.  
  11. CREATE TRIGGER trigger_votes AFTER INSERT ON votes FOR EACH ROW
  12. BEGIN
  13. UPDATE collections
  14. SET loves = CASE NEW.vote WHEN 1 THEN loves + 1 ELSE loves END
  15. ,hates = CASE NEW.vote WHEN 1 THEN hates ELSE hates + 1 END
  16. WHERE collections.id = NEW.collection_id;
  17. END $$
  18.  
  19. DELIMITER ;
Add Comment
Please, Sign In to add comment