sql - Difficulty with join 3 tables in a query in php -
my database has 3 tables wish access in select query cannot seem work. selecting 2 tables works fine know else working apart code selecting 3 tables. database has been created on phpmyadmin
the tables follows:
forum_replies
- reply_id
- topic_id
- user_id
- reply_text
- reply date
forum_topics
- topic_id
- category_id
- user_id
- topic_title
- topic_description
- topic_date
users
- user_id
- username
this code have tried use , shows fields wish select:
$queryreply = "select forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id, forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date forum_replies join forum_topics on forum_replies.topic_id = forum_topics.topic_id join users on forum_replies.user_id = users.user_id "; $result = mysql_query($queryreply) or die (mysql_error()); $row = mysql_fetch_array($result);
example in code appreciated.
use query:
select a.reply_text, a.reply_date, b.topic_title, c.username forum_replies left join forum_topics b on a.topic_id=b.topic_id left join users c on a.user_id=c.user_id // apply where, order, group if needed
apart syntax errors, should use left join
, table alias in case.
to show topic creator's username, can adjust query following:
select a.reply_text, a.reply_date, b.topic_title, c.username reply_user, (select username users user_id=b.user_id) topic_creator forum_replies left join forum_topics b on a.topic_id=b.topic_id left join users c on a.user_id=c.user_id // apply where, order, group if needed