Q:

Is it possible with MySQL to generate a query to fetch from several tables even if one has no results?

I'm facing a problem here:
I'm building a forum, this forum has several tables and I'm trying to fetch the comments and user info in a single query.
So far, it should be easy, the problem is that I can't change the structure and with the following query I get a perfect result IF there is a like to the answer. If no one likes the answer it fails. 
Select
  mfr.mfr_forum_answers.id,
  mfr.mfr_forum_answers.date_created,
  mfr.mfr_forum_answers.last_updated,
  mfr.mfr_forum_answers.content,
  mfr.mfr_forum_answers.accepted,
  mfr.mfr_forum_answers.user_id,
  mfr.mfr_users.level,
  mfr.mfr_users.avatar,
  mfr.mfr_forum_likes.subject_id,
  mfr.wp_users.ID As ID1,
  mfr.mfr_forum_topics.user_id As owner_id,
  (SELECT count(mfr.mfr_forum_likes.id) FROM mfr.mfr_forum_likes WHERE mfr.mfr_forum_likes.subject_id = :id AND mfr.mfr_forum_likes.type = 'answer') as likes,
  (SELECT count(mfr.mfr_forum_likes.id) FROM mfr.mfr_forum_likes WHERE mfr.mfr_forum_likes.subject_id = :id AND makefitreal.mfr_forum_likes.type = 'answer' AND mfr.mfr_forum_likes.user_id = :sessionId ) as i_like,
 mfr.wp_users.user_nicename
From
  mfr.mfr_forum_likes Inner Join
  mfr.mfr_forum_answers
    On mfr.mfr_forum_answers.topic_id =
    mfr.mfr_forum_likes.subject_id Inner Join
  mfr.mfr_users
    On mfr.mfr_forum_answers.user_id = mfr.mfr_users.id
  Inner Join
  mfr.wp_users
    On mfr.mfr_users.id = mfr.wp_users.ID Inner Join
  mfr.mfr_forum_topics
    On mfr.mfr_forum_answers.topic_id = mfr.mfr_forum_topics.id
Where
     mfr.mfr_forum_answers.topic_id = :id
And
     mfr.mfr_forum_likes.type = 'answer'

So far as said it returns only if an answer has a like, I'm thinking on adding a add to the user who posts the answer by default but I'm trying to improve my skills by solving new issues. 
If someone has a suggestion in how I could overcome the fact that if a table is empty, the query continues I'd be really thankfull. 
Thanks in advance-
Pihh

A:

Yes. What you are looking for are called left and right joins. According to the documentation, with a LEFT JOIN you still join two tables as normal but 

If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table.

This means that you can try to join two tables, but if a row does not have any results it will still return the results from the first table. The same is true for a RIGHT JOIN only it works the opposite way: it will return results if the tabled being joined to has results, but the original table does not.
It looks like you have 3 tables for 3 relationships: there are answers, a user gives an answer, and an answer might or might not have like. To grab this data, I would suggest starting from your answers table, performing an INNER JOIN on your users table (assuming there are always users), and a LEFT JOIN on your likes table. Here is a simple example:
SELECT *
FROM answers
INNER JOIN users ON users.id = answers.user_id
LEFT JOIN likes ON likes.answer_id = answer.id
WHERE answers.id = :id
AND likes.type = 'answers'

Of course, if for some unknown reason you need to start from your likes table, then you'd have to RIGHT JOIN the other tables. I hope that gives you a good idea of how you'd make your query.

