1

タグやコメントが書ける記事を載せたブログを作っています。1つのクエリを取得するために適切なSQLを取得しようとしています。私は4つのテーブルを持っています:

article
+------------+-----------------+------------+
| article_id | title           | photo      |
+------------+-----------------+------------+
|          1 | This is a test  | image1.jpg |
|          2 | Another Article | image2.jpg |
+------------+-----------------+------------+


article_tag
+------------+--------+ 
| article_id | tag_id |
+------------+--------+
|          1 |      1 | 
|          1 |      2 | 
|          2 |      2 |
+------------+--------+

tags
+--------+------+ 
| tag_id | name |
+--------+------+ 
|      1 | tag1 |
|      2 | tag2 |
+--------+------+

comment
+------+---------+------------+
| name | message | article_id |
+------+---------+------------+
|    1 | hello   |          1 |
|    2 | a       |          2 |
+------+---------+------------+

私はこれを取得しようとしています:

+------------+----------------+------------+---------+----------+
| article_id | title          | photo      | tag_ids | comments |
+------------+----------------+------------+---------+----------+
|          1 | This is a test | image1.jpg | 1,2     |        1 |
+------------+----------------+------------+---------+----------+

これは私がこれまでに持っているものです:

SELECT a.article_id, a.title, a.photo, a.date, a.description_long, a.author, GROUP_CONCAT(tag_id) as `tag_ids`, COUNT(c.comment_id) as comments
FROM article as a
JOIN article_tag as at
ON a.article_id = at.article_id
LEFT JOIN comment as c
ON a.article_id = c.article_id
WHERE a.article_id = 1

しかし、コメントは1ではなく2として表示されますか?ありがとうPS誰かがtag_idsを1,2からtag1、tag2に変更できる方法を知っているなら、それは素晴らしいでしょう:-)

4

2 に答える 2

1

タグとコメントは独立しています。したがって、3つのタグと2つのコメントがある場合、組み合わせで6行になります。

MySQLでこのクエリを修正する最も簡単な方法は、group byを実行し、selectでdistinctを使用することです。

SELECT a.article_id, a.title, a.photo, a.date, a.description_long, a.author,
       GROUP_CONCAT(distinct tag_id) as `tag_ids`, COUNT(distinct c.comment_id) as comments
FROM article a JOIN
     article_tag as at
     ON a.article_id = at.article_id LEFT JOIN
     comment c
     ON a.article_id = c.article_id
WHERE a.article_id = 1
group by a.article_id

ただし、クエリを修正する「適切な」方法は、結合を修正することです。fromこれは、 :内のサブクエリを使用します。

from . . .
     (select article_id, group_concat(tag_id) as tags
      from tags
      group by article_id
     ) at
     . . .
     (select article_id, count(*) as numComments
      from comments
      group by article_id
     ) c
     . . .
于 2013-02-23T22:41:31.560 に答える
0

これは、タグが1つ以上あるため、2つの行が生成されるためです。

COUNT(DISTINCT c.comment_id)を試してください。これは個別のコメントIDのみをカウントするため、重複するものは2回カウントされません。

タグ名を取得するには、タグテーブルで左結合を実行します。次に、tag_id列の代わりにname列を持つGROUP_CONCAT。

あるべき姿は次のとおりです。

SELECT a.article_id, a.title, a.photo, a.date, a.description_long, a.author, GROUP_CONCAT(d.name) AS `tag_names`, COUNT( DISTINCT c.comment_id ) AS comments
FROM article as a
JOIN article_tag as at
ON a.article_id = at.article_id
LEFT JOIN comment as c
ON a.article_id = c.article_id
LEFT JOIN tags d ON at.tag_id = d.tag_id
WHERE a.article_id = 1
于 2013-02-23T23:07:43.007 に答える