回答編集履歴
5
追記
test
CHANGED
@@ -127,3 +127,5 @@
|
|
127
127
|
)
|
128
128
|
|
129
129
|
```
|
130
|
+
|
131
|
+
ただ、unionしないと駄目な時点で、正規化からは外れています。
|
4
追記
test
CHANGED
@@ -65,3 +65,65 @@
|
|
65
65
|
|
66
66
|
|
67
67
|
[MySQL 5.7で生成カラムを使ってCHECK制約もどきを実装する](https://qiita.com/hmatsu47/items/ddf0fc74f2dc3bb7556f)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
追記
|
72
|
+
|
73
|
+
--
|
74
|
+
|
75
|
+
「友達かも」リストについては、事象を分けて考えれば、そんなに難しい事ではありません
|
76
|
+
|
77
|
+
```SQL
|
78
|
+
|
79
|
+
select * from user
|
80
|
+
|
81
|
+
where userid in (
|
82
|
+
|
83
|
+
select followingid from follow
|
84
|
+
|
85
|
+
where userid in ( -- 友達が承認された友達
|
86
|
+
|
87
|
+
select followingid from follow where status=1 and userid=1 -- 承認された友達
|
88
|
+
|
89
|
+
union all
|
90
|
+
|
91
|
+
select userid from follow where status=1 and followingid=1 -- 承認した友達
|
92
|
+
|
93
|
+
)
|
94
|
+
|
95
|
+
and status = 1
|
96
|
+
|
97
|
+
union all
|
98
|
+
|
99
|
+
select userid from follow
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
where followingid in ( -- 友達が承認した友達
|
104
|
+
|
105
|
+
select followingid from follow where status=1 and userid=1 -- 承認された友達
|
106
|
+
|
107
|
+
union all
|
108
|
+
|
109
|
+
select userid from follow where status=1 and followingid=1 -- 承認した友達
|
110
|
+
|
111
|
+
)
|
112
|
+
|
113
|
+
and status = 1
|
114
|
+
|
115
|
+
)
|
116
|
+
|
117
|
+
and userid NOT IN(1) -- 起点のユーザー
|
118
|
+
|
119
|
+
and userid not in ( -- 友達
|
120
|
+
|
121
|
+
select followingid from follow where status=1 and userid=1 -- 承認された友達
|
122
|
+
|
123
|
+
union all
|
124
|
+
|
125
|
+
select userid from follow where status=1 and followingid=1 -- 承認した友達
|
126
|
+
|
127
|
+
)
|
128
|
+
|
129
|
+
```
|
3
修正
test
CHANGED
@@ -14,15 +14,15 @@
|
|
14
14
|
|
15
15
|
where userid in (
|
16
16
|
|
17
|
-
select following from follow
|
17
|
+
select followingid from follow
|
18
18
|
|
19
|
-
where userid in (--友達が承認された友達
|
19
|
+
where userid in ( -- 友達が承認された友達
|
20
20
|
|
21
|
-
select following from follow where status=1 and userid=1 -- 承認された友達
|
21
|
+
select followingid from follow where status=1 and userid=1 -- 承認された友達
|
22
22
|
|
23
23
|
union all
|
24
24
|
|
25
|
-
select userid from follow where status=1 and following=1 -- 承認した友達
|
25
|
+
select userid from follow where status=1 and followingid=1 -- 承認した友達
|
26
26
|
|
27
27
|
)
|
28
28
|
|
@@ -32,13 +32,15 @@
|
|
32
32
|
|
33
33
|
select userid from follow
|
34
34
|
|
35
|
-
|
35
|
+
|
36
36
|
|
37
|
+
where followingid in ( -- 友達が承認した友達
|
38
|
+
|
37
|
-
select following from follow where status=1 and userid=1 -- 承認された友達
|
39
|
+
select followingid from follow where status=1 and userid=1 -- 承認された友達
|
38
40
|
|
39
41
|
union all
|
40
42
|
|
41
|
-
select userid from follow where status=1 and following=1 -- 承認した友達
|
43
|
+
select userid from follow where status=1 and followingid=1 -- 承認した友達
|
42
44
|
|
43
45
|
)
|
44
46
|
|
@@ -46,11 +48,11 @@
|
|
46
48
|
|
47
49
|
union all
|
48
50
|
|
49
|
-
select following from follow where status=1 and userid=1 -- 承認された友達
|
51
|
+
select followingid from follow where status=1 and userid=1 -- 承認された友達
|
50
52
|
|
51
53
|
union all
|
52
54
|
|
53
|
-
select userid from follow where status=1 and following=1 -- 承認した友達
|
55
|
+
select userid from follow where status=1 and followingid=1 -- 承認した友達
|
54
56
|
|
55
57
|
)
|
56
58
|
|
2
修正
test
CHANGED
@@ -44,11 +44,17 @@
|
|
44
44
|
|
45
45
|
and status = 1
|
46
46
|
|
47
|
+
union all
|
48
|
+
|
49
|
+
select following from follow where status=1 and userid=1 -- 承認された友達
|
50
|
+
|
51
|
+
union all
|
52
|
+
|
53
|
+
select userid from follow where status=1 and following=1 -- 承認した友達
|
54
|
+
|
47
55
|
)
|
48
56
|
|
49
57
|
and userid NOT IN(1)
|
50
|
-
|
51
|
-
|
52
58
|
|
53
59
|
```
|
54
60
|
|
1
追記
test
CHANGED
@@ -53,3 +53,7 @@
|
|
53
53
|
```
|
54
54
|
|
55
55
|
3.この構造で逆の組み合わせの一意制約は無理ですね、
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
[MySQL 5.7で生成カラムを使ってCHECK制約もどきを実装する](https://qiita.com/hmatsu47/items/ddf0fc74f2dc3bb7556f)
|