0

Following query returns 10 rows of recipe id's i wants to pass first query's output which is of 10 rows to the second query and display the output of both the queries inside a procedure.

Query 1:

SELECT 
    p.ID as recipe_ID,
    p.post_title as recipe_title,
    p.post_date as modified_date,
    p.post_name as recipe_url,
    CASE WHEN s.total_count THEN s.total_count ELSE 0 END as total_like_count,
    CASE WHEN c.bookmark=1 THEN 1 ELSE 0 END as bookmark_status,
    CASE WHEN c.like_status=1 THEN 1 ELSE 0 END as like_status,
    CASE WHEN pm.meta_value=1 THEN 1 ELSE 0 END as unlock_status

FROM
    posts p
LEFT JOIN social_val s on(p.ID=s.post_id)
LEFT JOIN social_status_connection as c on(p.ID=c.post_id AND c.user_id=1)
LEFT JOIN postmeta pm on(p.ID=pm.post_id AND pm.meta_value=1 AND pm.meta_key='recipe_lock'),
term_relationships as t, term_taxonomy tt
WHERE
  p.post_type = 'recipe'
    AND p.post_status = 'publish'   
    and p.ID = t.object_id
    and tt.term_id = 1279
    and t.term_taxonomy_id=tt.term_taxonomy_id     

ORDER BY modified_date DESC
LIMIT 0 , 10

Query 2 Below query is self join which generates image of above particular recipe id.

SELECT 
    p.guid as recipeImageURL
FROM
    posts p,
    posts m
where
    p.post_type = 'attachment'
        AND p.post_parent = m.ID
        AND m.ID = ID ORDER BY p.post_modified DESC
LIMIT 1;
7
  • so your question is ... "write a procedure" ???
    – mysql_user
    Dec 7, 2015 at 6:23
  • I have created separate procedure for above queries one for recipe details and one for image of particular recipe.My question is how to pass parameter of 1st query to 2nd query inside one procedure..is it possible?
    – Reena
    Dec 7, 2015 at 6:32
  • Possible... added answer
    – mysql_user
    Dec 7, 2015 at 6:44
  • Why don't you combine the two queries into one? Dec 7, 2015 at 7:03
  • because in WHERE clause p.post_type = 'recipe' AND p.post_status = 'publish' this condition is must. and in second query i have to use self join in that post_type='attachment' which return image url......I dont want to use sub query which will slow down 1st query....is there any other alternative using JOIN's?
    – Reena
    Dec 7, 2015 at 7:17

2 Answers 2

0

If you need to loop over the output of one query to another you can use cursors.

For eg, in this stored routine to find in all tables databases we have looped over:

DECLARE table_cur CURSOR FOR SELECT ...
OPEN table_cur;
table_loop:LOOP
...code...
END LOOP;
CLOSE table_cur;

So in your case declare cursor for first query and loop over each recipeid.

1
  • Try to do it without a cursor.
    – Rick James
    Dec 12, 2015 at 0:05
-1

I have tried with CURSOR inside single procedure:

First Procedure: Display recipe details

BEGIN SELECT p.ID as recipe_ID, p.post_title as recipe_title, p.post_date as modified_date, p.post_name as recipe_url, CASE WHEN s.total_count THEN s.total_count ELSE 0 END as total_like_count, CASE WHEN c.bookmark=1 THEN 1 ELSE 0 END as bookmark_status, CASE WHEN c.like_status=1 THEN 1 ELSE 0 END as like_status, CASE WHEN pm.meta_value=1 THEN 1 ELSE 0 END as unlock_status

              FROM
                  posts p
              LEFT JOIN social_val s on(p.ID=s.post_id)
              LEFT JOIN social_status_connection as c on(p.ID=c.post_id AND c.user_id=userid)
              LEFT JOIN postmeta pm on(p.ID=pm.post_id AND pm.meta_value=1 AND pm.meta_key='recipe_lock'),
              term_relationships as t, term_taxonomy tt
              WHERE
                p.post_type = 'recipe'
                  AND p.post_status = 'publish'   
                  and p.ID = t.object_id
                  and tt.term_id =termid
                  and t.term_taxonomy_id=tt.term_taxonomy_id        
               ORDER BY modified_date DESC
                LIMIT offset , 5;

CALL recipe_list(userid,termid,offset); //calling second procedure for image END

Second Procedure: (For recipe images) BEGIN DECLARE post_id int DEFAULT 0;

-- declare cursor for recipe_image_cursor DEClARE recipe_image_cursor CURSOR FOR SELECT p.ID as recipe_ID FROM posts p LEFT JOIN social_share_counts s on(p.ID=s.post_id) LEFT JOIN social_status_connection as c on(p.ID=c.post_id AND c.user_id=1) LEFT JOIN postmeta pm on(p.ID=pm.post_id AND pm.meta_value=1 AND pm.meta_key='recipe_lock'), term_relationships as t, term_taxonomy tt WHERE p.post_type = 'recipe' AND p.post_status = 'publish'
and p.ID = t.object_id and tt.term_id = 1279 and t.term_taxonomy_id=tt.term_taxonomy_id LIMIT 0 , 10;

OPEN recipe_image_cursor;

get_image: LOOP

FETCH recipe_image_cursor INTO post_id;

--Display Results SELECT p.guid as recipeImageURL FROM posts p, posts m where p.post_type = 'attachment' AND p.post_parent = m.ID AND m.ID = post_id ORDER BY p.post_modified DESC LIMIT 1; END LOOP get_image; CLOSE recipe_image_cursor; END

How can i display result of cursor in column format..is it possible? or which is the better way?..i am new with procedures and cursor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.