回答編集履歴
3
推敲
answer
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
```
|
17
17
|
ただ、データ上GROUP_CONCAT()でDistinctが必要な状況ならDistinctが不要になるようにしておいた方が良いと思われます。
|
18
18
|
|
19
|
-
質問にある、「
|
19
|
+
質問にある、「tag='大阪'」を含むデータの抽出は以下です。
|
20
20
|
```SQL
|
21
21
|
SELECT at.a_id
|
22
22
|
, GROUP_CONCAT(DISTINCT t.tag_id)
|
2
推敲
answer
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
FROM a_tag at
|
25
25
|
inner join tag t
|
26
26
|
ON t.tag_id = at.tag_id
|
27
|
-
WHERE at.a_id in (select a_id from a_tag where
|
27
|
+
WHERE at.a_id in (select a_id from a_tag where tag = '大阪')
|
28
28
|
GROUP BY at.a_id
|
29
29
|
```
|
30
30
|
または
|
@@ -35,6 +35,6 @@
|
|
35
35
|
FROM a_tag at
|
36
36
|
inner join tag t
|
37
37
|
ON t.tag_id = at.tag_id
|
38
|
-
WHERE exists (select 1 from a_tag where
|
38
|
+
WHERE exists (select 1 from a_tag where tag = '大阪' and a_id = at.a_id)
|
39
39
|
GROUP BY at.a_id
|
40
40
|
```
|
1
追記
answer
CHANGED
@@ -14,4 +14,27 @@
|
|
14
14
|
WHERE at.a_id = 4
|
15
15
|
GROUP BY at.a_id
|
16
16
|
```
|
17
|
-
ただ、データ上GROUP_CONCAT()でDistinctが必要な状況ならDistinctが不要になるようにしておいた方が良いと思われます。
|
17
|
+
ただ、データ上GROUP_CONCAT()でDistinctが必要な状況ならDistinctが不要になるようにしておいた方が良いと思われます。
|
18
|
+
|
19
|
+
質問にある、「tag_id='大阪'」を含むデータの抽出は以下です。
|
20
|
+
```SQL
|
21
|
+
SELECT at.a_id
|
22
|
+
, GROUP_CONCAT(DISTINCT t.tag_id)
|
23
|
+
, GROUP_CONCAT(DISTINCT t.tag)
|
24
|
+
FROM a_tag at
|
25
|
+
inner join tag t
|
26
|
+
ON t.tag_id = at.tag_id
|
27
|
+
WHERE at.a_id in (select a_id from a_tag where tag_id = '大阪')
|
28
|
+
GROUP BY at.a_id
|
29
|
+
```
|
30
|
+
または
|
31
|
+
```SQL
|
32
|
+
SELECT at.a_id
|
33
|
+
, GROUP_CONCAT(DISTINCT t.tag_id)
|
34
|
+
, GROUP_CONCAT(DISTINCT t.tag)
|
35
|
+
FROM a_tag at
|
36
|
+
inner join tag t
|
37
|
+
ON t.tag_id = at.tag_id
|
38
|
+
WHERE exists (select 1 from a_tag where tag_id = '大阪' and a_id = at.a_id)
|
39
|
+
GROUP BY at.a_id
|
40
|
+
```
|