Jump to content

JOIN 2 tables, with 2 columns


MasterACE14

Recommended Posts

I have 2 tables:

= feed =
- feed_id (PK)
- user_id (FK)  (user who owns this particular record on his feed)
- poster_id (FK) (user who posted the record, can be the same as `user_id`)
- feed_message

= accounts = 
- id (PK)
- username 

 

Now I'm trying to select all records that belong to "user_id = 2" including the `username` of the `poster_id`. I've got my query selecting all the correct records, but the `username` is null for any records that don't have the same `user_id` and `poster_id`

 

Query:

SELECT `accounts`.`username`, `feed`.`feed_message` 
FROM `feed` 
LEFT JOIN `accounts` ON (accounts.id = feed.poster_id) 
WHERE feed.user_id = 2

 

Result:

username  	feed_message
ACE	 test
ACE	 test
ACE	 test
ACE	 test
ACE	 test
ACE	 test
ACE	 test
ACE	 testing
ACE	 testing
ACE	UPDATING MY FEED
ACE	 tester
NULL	test
NULL	message from player 'test'

 

Not sure where I'm going wrong?

 

Thanks,

Ace

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.