Possible Duplicate:
SQL left join vs multiple tables on FROM line?
SELECT messages.message_title,
messages.message_content,
messages.message_timestamp,
user_message_relations.sender_id
FROM messages
LEFT JOIN global_messages ON messages.message_id = global_messages.message_id
LEFT JOIN user_messages ON messages.message_id = user_messages.message_id, user_message_relations
WHERE user_message_relations.receiver_id = 3
OR
SELECT messages.message_title,
messages.message_content,
messages.message_timestamp,
user_message_relations.sender_id
FROM messages,
global_messages,
user_messages,
user_message_relations
WHERE user_message_relations.receiver_id = 3
My main question is, what’s the point of using LEFT OUTER JOIN (or any kind of JOIN) if I can just call the table directly like the second query? Is there a benefit?
I see that the second method is not considered “best practice” … with that in mind, would this following query be correct if I wanted to populate the inbox of a user with the id of 3?
SELECT messages.message_id,
messages.message_title,
messages.message_content,
messages.message_timestamp,
user_messages.message_id,
user_message_relations.sender_id
FROM user_message_relations
INNER JOIN user_messages ON user_message_relations.user_message_id = user_messages.user_message_id
INNER JOIN messages ON user_messages.message_id = messages.message_id
WHERE user_message_relations.receiver_id = 3
As OMG Ponies says, there’s no such thing as “calling tables” directly. In SQL, multiple tables can be be combined in three ways:
As it happens, your three examples each has one of these, in the order I listed them. None is more “efficient” than the other; they return very different results!
The confusion comes because there are two different syntaxes for these; your example 2 is using the older SQL89 syntax in which table names are separated by commas, and the combination is a Cartesian product unless something in the WHERE clause says to join them.