0

我有这个要求,我正在使用 MySQl 数据库

评论表

id  user id  ariticle_id     comment        comment date
========================================================
 1     45        2           comment1          02/03/12
 2     45        2           comment2          03/03/12
 3     45        2           comment3          04/03/12 
 4     45        2           comment4          05/03/12
 5     45        2           comment5          06/03/12
 6     45        5           comment7          04/03/12
 7     45        5           comment8          05/03/12
 8     45        5           comment9          06/03/12

文章表

id   article_text               article_date
============================================
2    Some text                  01/03/12
2    updated text               04/03/12
5    another text               03/03/12
5    another updated text       04/03/12

我想从评论表和文章表中获取所有详细信息 userid、article_id、article_text、comment、comment_date

对于给定的文章 id =2 用户 id =45,并在更新文章后提供详细信息。

预期的输出是这样的

 user_id   article_id   comment           commentdate
     45        2           comment3          04/03/12
     45        2           comment4          05/03/12
     45        2           comment5          06/03/12

我迷路了请帮忙,我正在尝试使用 MYSQL 中的查询

4

3 回答 3

2

您应该尝试具体说明您正在使用的 id,试试这个,

Select * from comment_table
join article_table on article_table.id = comment_table.id
于 2012-05-07T06:23:07.687 回答
1

现在,我想我明白了你的要求

但是文章表中有重复的ID

修复此问题后,您可以尝试使用此 SQL

SELECT  c.userid,
        c.article_id,
        c.comment,
        c.commentDate
FROM   comment_table c 
INNER JOIN article_Table a
ON     a.id = c.ariticle_id
AND    c.article_id = 2 
AND    c.userid = 45 

WHERE   c.commentDate >= (SELECT article_date
                          FROM article_date
                          WHERE id = c.ariticle_id )
于 2012-05-07T08:03:39.537 回答
0

希望这可以帮助:

SELECT  a.userid,
        a.article_id,
        b.article_text,
        a.comment,
        a.commentDate
FROM    comment_table a INNER JOIN article_Table b
           ON a.article_ID = b.article_ID
WHERE   a.article_id = 2 AND a.userid = 45 AND
            b.commentdate > date('2012/04/03')
于 2012-05-07T06:22:01.343 回答