SELECT * FROM tblPosts WHERE tblPosts.usersID = (SELECT tblFriends.friendsID WHERE tblFriends.myID = '$myID') ORDER BY tblPosts.date DESC;

Advertisements
SELECT * FROM tblPosts WHERE tblPosts.usersID = (SELECT tblFriends.friendsID WHERE tblFriends.myID = '$myID') ORDER BY tblPosts.date DESC;

This doesn’t through an error but it also doesn’t work. I think I need a join statement maybe? There are several fields in tblPost. ID, Comment, Date, userID, etc… Using my $myID variable I want to output <<<while($row = $result->fetch_assoc())>>> all the matching rows from tblPosts WHERE tblPosts.usersID equals every friend in my tblFriends.friendsID column when tblFriends.myID = ‘$myID’ and then ORDER BY tblPosts.date DESC? Any help would be great! Thank you.

>Solution :

Just join your tables:

select p.*
from tblFriends f
join tblPosts p on p.userID = f.friendsID
where f.myID = '$myID'
order by p.date desc;

Also note the use of short table aliases makes for a more compact and easier to read query.

Leave a ReplyCancel reply