0

I am developing a social website.I have an option called alerts which shows pending friend requests and unread messages.My query is following:

SELECT
  sk_friends.frndship_from_user_id,
  sk_messages.msg_from_user_id,
  sk_messages.msg_text,
  sk_messages.msg_date 
FROM
  sk_friends INNER JOIN sk_messages
WHERE 
  sk_messages.msg_to_user_id = '$user_id'
  AND sk_friends.frndship_to_user_id ='$user_id'  
  AND sk_friends.frndship_status = 'pending'
  AND sk_messages.msg_status='unread'
ORDER BY 
  sk_friends.fndship_date ASC,
  sk_messages.msg_date ASC;
  • sk_friends and ak_messages are tables.
  • msg_from_user_id is the id of sender
  • frndship_from_user_id is the id of the user who sends the request
  • $user_id is the id of the login user

Each row data is appearing twice. I dont know why does it happen.

3
  • Can you make a small fiddle and explain? Commented Feb 12, 2013 at 10:19
  • 4
    you have missed ON statement in join query. check dev.mysql.com/doc/refman/5.0/en/join.html to apply inner join. Commented Feb 12, 2013 at 10:22
  • Use "ON" in place of "WHERE" in the JOIN-segment of your query. Commented Feb 12, 2013 at 10:23

2 Answers 2

4

Your inner join does not have an ON clause. Try adding one, that should remove double results.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

SELECT 
    sf.frndship_from_user_id,
    sm.msg_from_user_id,
    sm.msg_text,sm.msg_date 
FROM 
    sk_friends sf ,
    sk_messages sm 
WHERE 
    sm.msg_to_user_id = sf.frndship_to_user_id AND
    sm.msg_to_user_id = '$user_id'  AND     
    sf.frndship_status = 'pending' AND 
    sm.msg_status='unread' 
ORDER BY 
    sf.fndship_date ASC,sm.msg_date ASC;

Comments

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.