PHP JOIN结果中有两个MySQL表

时间:2015-09-02 11:18:03

标签: php mysql join

我有两个用于插入评论数据的MySQL表,并审查这样的数据:

注释:

|id|post_id|text|name|timestamp|approved|parent_id|type|ip|

评论:

|id|postID|comments_id|reviewfield1|reviewfield2|reviewfield3

在行动中我为每条评论插入一条评论。现在,我需要针对评论为name的每条评论显示/打印带有评论textreviewfield1 + reviewfield2 reviewfield3 approved的评论列表像这样:

 commenter 1 
  text
  reviewfield1
  reviewfield2
  reviewfield3

TRY:

"SELECT name,text,timestamp FROM " . COMMENTS . " LEFT JOIN " . REVIEWS . " WHERE " . COMMENTS . ".id = " . REVIEWS . ".comments_id AND
        post_id = ? AND type = ? AND approved = 1 ", $id, $type"

但这不适合我。如何使用PHP JOIN方法生成此内容?

1 个答案:

答案 0 :(得分:1)

您在查询中犯了错误。必须是这样的:

"SELECT name,text,timestamp,reviewfield1 FROM " . COMMENTS . " LEFT JOIN " . REVIEWS . " ON " . COMMENTS . ".id = " . REVIEWS . ".comments_id WHERE
        post_id = ? AND type = ? AND approved = 1 ", $id, $type"

您可以从第二个(已加入的)表中选择行,与第一个表相同。