brendan
brendan

Reputation: 19

select rows with condition in multi rows of other table

I want to show all posts that user had followed. I have two tables are post_tb and follow_tb, linked column is post_tb.user_id and follow_tb.follower_id.

if i show all posts on the table i got sql condition like this

"select * from post_tb order by date_time desc;"

And this is sql condition that show only posts people they followed.

"select * from post_tb where user_id in(select follow_to_id from follow_tb where follow_tb.follower_id='$sessionid') order by date_time des;"

Both of them is work, but the second sql is every slow when number of records increase. Is there the better way?
thanks for your answer

Upvotes: 0

Views: 48

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

Sub queries are slow instead use join

select * from post_tb  p
join follow_tb f ON(p.user_id = f.follow_to_id )
where f.follower_id='$sessionid'
order by date_time desc;

Upvotes: 1

Related Questions