Order results from two queries based on time in second query [closed]

Use a single query that joins the two tables, instead of doing nested loops.

SELECT p.* FROM posts AS p
JOIN friends AS f ON p.postby = f.friendId
ORDER BY p.posttime DESC

From your comment, it appears your first query isn’t so simple. But you can join with a subquery.

SELECT p.* FROM posts AS p
JOIN (
    SELECT user_from AS friendId
    FROM friend_requests 
    WHERE user_to = $SessionID AND accepted = '1'
    UNION
    SELECT user_to AS friendId
    FROM friend_requests
    WHERE user_from = $SessionID AND accepted = '1'
) AS f ON p.postby = f.friendId
ORDER BY p.posttime DESC

Browse More Popular Posts

Leave a Comment