回答編集履歴

1

付き

2019/08/02 12:42

投稿

yambejp
yambejp

スコア114856

test CHANGED
@@ -13,3 +13,53 @@
13
13
 
14
14
 
15
15
  もっと効率的な処理が必要かもしれませんが、とりあえずダブりはなくなるでしょう
16
+
17
+
18
+
19
+ # 追記
20
+
21
+ sample確認しました。
22
+
23
+ これはtagとcategoryを別々に処理してサブクエリでつなぐのが楽でしょう
24
+
25
+ ```SQL
26
+
27
+ SELECT *
28
+
29
+ FROM locations as t1
30
+
31
+ left join(
32
+
33
+ /* ここからtagまとめ */
34
+
35
+ select t2.location_id as id,group_concat(t3.name) as tags
36
+
37
+ from location_tags as t2
38
+
39
+ inner join tags as t3 ON t2.tag_id = t3.id
40
+
41
+ group by t2.location_id
42
+
43
+ /* ここまで */
44
+
45
+ ) as t6 using(id)
46
+
47
+ left join (
48
+
49
+ /* ここからcategoryまとめ */
50
+
51
+ select t4.location_id as id,group_concat(t5.name) as categories
52
+
53
+ from location_categories as t4
54
+
55
+ inner join categories as t5 ON t4.category_id = t5.id
56
+
57
+ group by t4.location_id
58
+
59
+ /* ここまで */
60
+
61
+ ) as t7 using(id)
62
+
63
+ order by id
64
+
65
+ ```