Techy
Techy

Reputation: 2654

Issue in mysql Join query

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;

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

Upvotes: 0

Views: 58

Answers (2)

Chintan
Chintan

Reputation: 1204

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;

Upvotes: 0

Husman
Husman

Reputation: 6909

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

Upvotes: 4

Related Questions