Abdul Muheet
Abdul Muheet

Reputation: 315

How to get values from two table with subqueries?

I have two tables names are feed, and friendship....

friendship table has these fields..toid which the session user id, fromid which is the friend id, and status that may be 0 or 1, and the feed table contains id,post_user_id which is the feed poster id, and contents. Now i want to get the feed from the feed table only from my friends i am able to get feeds from my friends but i can't get my own feed , when i put my own id in the condition the query is executed but the result empty. i tried these

    SELECT * FROM feed WHERE `post_user_id` IN (Select fromid from 
     gr_user_friendships where toid = 47) ORDER BY 
      post_date_time DESC 

/****This query giving only the friends records not mine to get my records also i put my own id in the condition agains****/

   SELECT * FROM feed WHERE `post_user_id` IN (Select fromid from 
     gr_user_friendships where toid = 47) and `post_user_id` = 47 ORDER
      BY post_date_time DESC 

/*This time this results empty**/

I also tried this with inner join no success at all

Upvotes: 0

Views: 88

Answers (4)

Jackie Dong
Jackie Dong

Reputation: 813

Try:

SELECT * FROM feed WHERE post_user_id = 47 or 'post_user_id' IN 
(Select fromid from gr_user_friendships where toid = 47) ORDER BY 
 post_date_time DESC 

Upvotes: 1

amith.gotamey
amith.gotamey

Reputation: 988

In your second query:

SELECT * FROM feed WHERE `post_user_id` IN (Select fromid from 
     gr_user_friendships where toid = 47) and `post_user_id` = 47 ORDER
      BY post_date_time DESC

just replace and with or

Upvotes: 1

Max Schindler
Max Schindler

Reputation: 380

Shouldn`t it work with an OR instead of the AND?

SELECT * FROM feed WHERE `post_user_id` IN (Select fromid from 
 gr_user_friendships where toid = 47) OR `post_user_id` = 47 ORDER
  BY post_date_time DESC

Upvotes: 3

Bipin Kareparambil
Bipin Kareparambil

Reputation: 473

Try this..

SELECT * FROM feed INNER JOIN (Select fromid from 
 gr_user_friendships where toid = 47) WHERE 'toid' =  47 and `post_user_id` =    47 ORDER
  BY post_date_time DESC 

Upvotes: 0

Related Questions