2017-04-07 7 views
0

としてミリメートルテーブルから生じる:のMySQL:私はこの​​約2つの質問有するコマ分離カラム

1.)なぜ結果リスト内の行2です。私は、これは= ONLY_FULL_GROUP_BYのsql_modeと互換性がありません

SELECTリストは、非集約列が含まれているエラーを得た)生産(mysqlの5.7をオン

2)...

のMySQL 5.6スキーマのセットアップ

CREATE TABLE MM(
    `post_id` int, 
    `tag_id` int 
); 

CREATE TABLE Post 
(
    `post_id` int, 
    `name` varchar(200) 
); 

CREATE TABLE Tag(
    `tag_id` int, 
    `tagname` varchar(200) 
); 

Insert into Post values (1, "First Post"); 
Insert into Post values (2, "Second Post"); 

Insert into Tag values (1, "sql"); 
Insert into Tag values (2, "mm relation"); 
Insert into Tag values (3, "group concat"); 

Insert into mm values (1, 1); 
Insert into mm values (1, 2); 
Insert into mm values (1, 3); 

クエリ1

Select 
    Post.post_id, 
    Post.name, 
    GROUP_CONCAT(t.tagname SEPARATOR ',') as tags 
    from Post 
left join 
    MM on MM.post_id = Post.post_id 
left join 
    Tag as t on t.tag_id = MM.tag_id 

Results

| post_id |  name |       tags | 
|---------|------------|------------------------------| 
|  1 | First Post | sql,mm relation,group concat | 

答えて

0

あなたはポストごとにグループ化されたタグを取得する必要がある場合はGROUP BY Post.post_idを使用する必要があります。

Select 
    Post.post_id, 
    Post.name, 
    GROUP_CONCAT(t.tagname SEPARATOR ',') as tags 
    from Post 
left join 
    MM on MM.post_id = Post.post_id 
left join 
    Tag as t on t.tag_id = MM.tag_id 
GROUP BY Post.post_id 

をそして、あなたはまた、あなただけのユニークなキーワードを取得することを確認するためにDISTINCTを使用することができます。 http://sqlfiddle.com/#!9/e7c87e/12

関連する問題