The join is not where it goes wrong.
safra;11012183 wrote:
FROM comments as t1
LEFT JOIN users as t2 ON t1.user=t2.ID
LEFT OUTER JOIN favorites as t3 ON t3.favorite=t2.ID
OUTER and INNER joins specify which information to retain for rows having no matches in the other table (LEFT, RIGHT and FULL outer joins respectively). However, you should note that the difference between INNER JOIN and LEFT JOIN is exactly that of the first being an inner join while the second is an outer. INNER JOIN is shorthand notation for LEFT INNER JOIN, while LEFT JOIN is shorthand notation for LEFT OUTER JOIN. Thus you have not modified your query at all by adding OUTER to your left join. Also note that you should allready have the expected behaviour (include everything from the left hand side i.e. the comments table joined with users, wether or not there are matching rows in the favourites table, while not including favourites information if there is no comments info).
On the other than, you might as well switch the first join from LEFT JOIN to INNER JOIN to remove all comments for which there is no user. Granted, it's a case which should NEVER exist - After all, you have specified FOREIGN KEY (user) REFERENCES users ON DELETE CASCADE on the comments table havn't you?
safra;11012183 wrote:
WHERE t1.date<'...' AND t1.date>'...' AND t3.user='this user ID'
You are however specifying the the favourite has to match 'this user ID' (hint, when posting rather write that as @this_user_id, since that would signify a variable in SQL rather than a string literal which makes it more straightforward to understand), which you don't seem to be looking for. You seem to be looking for comments for @this_user_id, which would mean
t1.user = @this_user_id
Also, to make it clear what table is referred when using aliases, for your own sake do use meaningful names (or at least meaningful initials)
FROM comments AS c
-- (left) inner join instead of left (outer) join
INNER JOIN users AS u
-- left (outer) join instead of left outer join
LEFT JOIN favorites AS fav
-- which will now make it clear that you are matching user id against
-- the favourites table... which is clearly wrong
WHERE fav.id = @this_user_id