naraku9333

Untitled

Dec 15th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.95 KB | None | 0 0
  1. /*MySQL - stored procedure*/
  2. DELIMITER //
  3. DROP PROCEDURE IF EXISTS notedisplay;
  4. CREATE PROCEDURE `notedisplay` ()
  5. BEGIN
  6.     /* Get the combinations of NoteId with their Tag Ids*/
  7.     CREATE TEMPORARY TABLE NAT ( noteid INT , tagname VARCHAR(100));
  8.     INSERT INTO NAT
  9.     SELECT     NoteTags.note_id, LTRIM(RTRIM(Tag.text))
  10.     FROM NoteTags INNER JOIN Tag ON NoteTags.tag_id = Tag.tag_id;
  11.  
  12.     -- Combine the Tags with ':' for the groups of NoteIds
  13.     CREATE TEMPORARY TABLE NTD ( noteid INT , tags VARCHAR(100));
  14.     INSERT INTO NTD
  15.     SELECT noteid, group_concat(tagname separator ':') AS tags
  16.     FROM NAT
  17.     WHERE NAT.noteid IS NOT NULL
  18.     GROUP BY NAT.noteid;
  19.  
  20.     /* Join the above to the Notes Table and throw the result*/
  21.     SELECT
  22.           Notes.note_id AS ID
  23.         , Notes.title AS Title
  24.         , Notes.body AS Text
  25.         , Notes.created AS Created
  26.         , Notes.updated AS Updated
  27.         , NTD.tags AS Tags
  28.  
  29.     FROM NTD
  30.     INNER JOIN Notes ON NTD.noteid = Notes.note_id;
  31.  
  32. END //
  33. DELIMITER ;
Advertisement
Add Comment
Please, Sign In to add comment