0

I want to fetch data from 3 tables pm_conv,user,photo but after joining the 3rd table photo i get multiples rows i tried allot to get data in LIMIT 1 but cant .

Here is the query

SELECT 
    pm_conv. *, user.username, user.id, photo.url 
FROM 
    pm_conv 
JOIN 
    user ON CASE 
        WHEN pm_conv.sender_id ='2869' 
            THEN pm_conv.recipient_id = user.id 
        WHEN pm_conv.recipient_id ='2869' 
            THEN pm_conv.sender_id = user.id 
        END
JOIN 
    photo ON CASE 
        WHEN pm_conv.sender_id ='2869' 
            THEN pm_conv.recipient_id = photo.user_id 
        WHEN pm_conv.recipient_id ='2869' 
            THEN pm_conv.sender_id = photo.user_id 
        END
WHERE 
    `sender_id`='2869' 
    OR `recipient_id` ='2869' 
ORDER BY  
    `last_answer_date` DESC
12
  • Do you getting any error ? Commented Jul 10, 2017 at 5:32
  • What do you want to achieve? Considering it's a conversation and your query means you want to get all the messages involving user 2869, you will really get multiple results. What I don't understand is, you said that even though you used limit 1, you're still getting multiple results? You probably misplaced your LIMIT 1. Commented Jul 10, 2017 at 5:33
  • Yes its a 3 table based on converstion . I want to fetch conv,username and photo respectively from these tables But photo table gives multiple rows because it has more than one photo against one id Commented Jul 10, 2017 at 5:38
  • What is your condition on which photo the user is currently using? Do you have another relevant column in photo table that might address this question? Commented Jul 10, 2017 at 5:42
  • i m matching the id from pm_conv table to photo's user_id colum e.g id =3562 and in photo.user_id has 4 rows with the column user_id Commented Jul 10, 2017 at 5:46

2 Answers 2

1

Try subquery instead , for photo table. May be it should work.

SELECT 
        pm_conv. *, 
        user.username, 
        user.id, 
        IF(pm_conv.sender_id ='2869', 
            (SELECT photo.user_id FROM photo WHERE pm_conv.recipient_id = photo.user_id LIMIT 1 ) , 
            IF (pm_conv.recipient_id ='2869', 
                (SELECT photo.user_id FROM photo WHERE pm_conv.recipient_id = photo.user_id LIMIT 1 ), 
                '')) as PHOTO_USER
    FROM 
        pm_conv 
    JOIN 
        user ON CASE 
            WHEN pm_conv.sender_id ='2869' 
                THEN pm_conv.recipient_id = user.id 
            WHEN pm_conv.recipient_id ='2869' 
                THEN pm_conv.sender_id = user.id 
            END

    WHERE 
        `sender_id`='2869' 
        OR `recipient_id` ='2869' 
    ORDER BY  
        `last_answer_date` DESC
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You so much sir :) You saved my day :)
Pleasure is all mine. :)
0

Just try this

SELECT 
    pm_conv. *, user.username, user.id, photo.url 
FROM 
    pm_conv 
JOIN 
    user ON CASE 
        WHEN pm_conv.sender_id ='2869' 
            THEN pm_conv.recipient_id = user.id 
        WHEN pm_conv.recipient_id ='2869' 
            THEN pm_conv.sender_id = user.id 
        END
JOIN 
    photo ON CASE 
        WHEN pm_conv.sender_id ='2869' 
            THEN pm_conv.recipient_id = photo.user_id 
        WHEN pm_conv.recipient_id ='2869' 
            THEN pm_conv.sender_id = photo.user_id 
        END
WHERE 
    `sender_id`='2869' 
    OR `recipient_id` ='2869' 
ORDER BY  
    `last_answer_date` DESC limit 1

Modified query:-

SELECT 
    pm_conv. *, user.username, user.id, photo.url 
FROM 
    pm_conv 
JOIN 
    user ON CASE 
        WHEN pm_conv.sender_id ='2869' 
            THEN pm_conv.recipient_id = user.id 
        WHEN pm_conv.recipient_id ='2869' 
            THEN pm_conv.sender_id = user.id 
        END
JOIN ((SELECT `photo`.* FROM `photo` join `pm_conv` as pmc ON CASE 
        WHEN pmc.sender_id ='2869' 
            THEN pmc.recipient_id = photo.user_id 
        WHEN pmc.recipient_id ='2869' 
            THEN pmc.sender_id = photo.user_id 
        END limit 1)) as photo
     ON CASE 
        WHEN pm_conv.sender_id ='2869' 
            THEN pm_conv.recipient_id = photo.user_id 
        WHEN pm_conv.recipient_id ='2869' 
            THEN pm_conv.sender_id = photo.user_id 
        END
WHERE 
    `sender_id`='2869' 
    OR `recipient_id` ='2869' 
ORDER BY  
    `last_answer_date` DESC

I thing it will give your desired output.

4 Comments

Thanks for your response. But it limits the whole data i just want to limit photo.url
Okay your photo.url have repeated value in table.
yes...because its has more urls of image against each id
@ArslanAli just try the modified query it will help you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.