Stranger
Stranger

Reputation: 10610

SELECT a single record with JOIN

i want to select a single record using from two tables. But if you use SIMPLE JOIN, it will only returns the value when the condition satisfies both the table and when we use LEFT or RIGHT JOIN also, it will return all the values from the LEFT or RIGHT table.

I'm using the following query,

SELECT * FROM activity_feeds LEFT OUTER JOIN activity_traders ON activity_feeds.feed_id = activity_traders.trdr_feed_id = '11'

I need to get the values from both tables only when activity_feeds.feed_id = activity_traders.trdr_feed_id = '11' and when the value does not matches with the right table, it should return null for the columns in the right table. Please help!

Edit:Table structures added

Feeds

enter image description here

Traders

enter image description here

Upvotes: 0

Views: 95

Answers (3)

John Woo
John Woo

Reputation: 263693

I assume that all the records are based from activity_feeds whether or not the feed_id exists on activity_traders table as long as it will display all feed_id that has a value of 11. Try this one.

SELECT * 
FROM   activity_feeds a
           LEFT OUTER JOIN  activity_traders b
                ON a.feed_id = b.trdr_feed_id 
WHERE  a.feed_id = '11'

Upvotes: 0

Lajja Thaker
Lajja Thaker

Reputation: 2041

SELECT * FROM activity_feeds LEFT OUTER JOIN activity_traders ON activity_feeds.feed_id = 
activity_traders.trdr_feed_id where activity_traders.trdr_feed_id  = '11'

Upvotes: 1

sel
sel

Reputation: 4957

ON activity_feeds.feed_id = activity_traders.trdr_feed_id 
WHERE activity_traders.trdr_feed_id = '11'

Upvotes: 0

Related Questions