2

テーブル Documents(id, name) と Tags(tagId, tagname, docId(FK)) を結合したい。ただし、1 つのドキュメントに多くのタグを含めることができるため、結果は多くの冗長な行を返します。

Document_name |Tag
----------------------
document01     tag01
document01     tag02
document02     tag01

私が解決しようとしているのは次のとおりです。

document_name |Tags
----------------------------
document01    | tag01, tag02
document02    | tag01

または次のようにすることができます:

Document_name |Tag_1   |Tag_2  |Tag_...
---------------------------------------
document01     tag01  |tag02
document02     tag01

このケースを実装する方法を知っている人はいますか? どうもありがとう!(このサイトで他の答えを見つけようとしていますが、この場合の検索に適したキーワードがわかりません)

4

3 に答える 3

2

MySQL を使用している場合は、次のようにできます。

select document_name, group_concat(tag SEPARATOR ', ')
from Documents
group by document_name
于 2012-07-05T06:14:36.103 に答える
1

MS-SQLの場合、これを試してください

select 
    d1.Document_name, 
    ( 
        select d2.Tag +','
        from Docs d2
        where d2.Document_name = d1.Document_name
        for xml path('')
    ) as Tags
from Docs d1
group by d1.Document_name
于 2012-07-05T06:30:28.337 に答える
0

MySQL では:

SELECT doc.name, GROUP_CONCAT( tag.tagname SEPARATOR ', ') 
       FROM documents AS doc
       JOIN Tags AS tag ON doc.id = tag.docId
       GROUP BY tag.docId

結果:


document_name |Tags
----------------------------
document01    | tag01, tag02
document02    | tag01
于 2012-07-05T06:50:16.733 に答える