Well let\'s see, the query I have is working fine, as soon as a friendpost is done, however. If the user has no friends, no result will be returned, and that\'s what I am trying
There are 2 problems:
LEFT JOIN
on friends. A LEFT JOIN
says to return all records from the first table in the join even if there are no results found in the second table in the join.
You also should the WHERE
clause conditions relating to friends
into the LEFT JOIN
clause, so that the conditions occur at the join. You should also be using m.id
wherever possible in your joins instead of $myId
to eliminate redundancy.JOIN
so they are easier to read.Example (Edited to add posts from friends, as well):
$query = "SELECT DISTINCT `u`.`id`, `p`.`byuser`, `p`.`newpost`, `p`.`id`, `p`.`postdate`
FROM `users` AS `u`
LEFT JOIN `friends` AS `f`
ON `f`.`userid` = `u`.`id`
OR `f`.`friendid` = `u`.`id`
JOIN `pinnwand` AS `p`
/* This will get all posts made by the user */
ON `p`.`byuser` = `u`.`id`
/* This will get all posts made TO the user by friends */
OR (`p`.`byuser` IN (`f`.`userid`, `f`.`friendid`)
AND `p`.`touser` = `u`.`id`)
WHERE `u`.`id` = {$myId}
AND `p`.`publicp` < 3
AND `p`.`typ` = 2
ORDER BY `p`.`id` DESC
LIMIT {$limit}, 10";