回答編集履歴

4

ちょうせい

2019/08/30 07:56

投稿

yambejp
yambejp

スコア114878

test CHANGED
@@ -33,8 +33,6 @@
33
33
  - 1,2,10順番に
34
34
 
35
35
  ```SQL
36
-
37
- select * from tbl where find_in_set('1',text);
38
36
 
39
37
  select * from tbl where text regexp('(^|,)1(,|$)');
40
38
 

3

tuiki

2019/08/30 07:56

投稿

yambejp
yambejp

スコア114878

test CHANGED
@@ -57,3 +57,79 @@
57
57
  select * from tbl where find_in_set('10',text);
58
58
 
59
59
  ```
60
+
61
+
62
+
63
+ # 効率的なデータの持ち方
64
+
65
+
66
+
67
+ ちゃんと正規化するならこうですね
68
+
69
+ - 元データ
70
+
71
+ ```SQL
72
+
73
+ create table id_text(pid int primary key,id int,text int,unique(id,text));
74
+
75
+ insert into id_text values
76
+
77
+ (1,1,1),
78
+
79
+ (2,2,1),
80
+
81
+ (3,2,2),
82
+
83
+ (4,2,3),
84
+
85
+ (5,3,10),
86
+
87
+ (6,3,11),
88
+
89
+ (7,4,1),
90
+
91
+ (8,4,10),
92
+
93
+ (9,4,12),
94
+
95
+ (10,5,10),
96
+
97
+ (11,5,11),
98
+
99
+ (12,5,12),
100
+
101
+ (13,6,2),
102
+
103
+ (14,7,10),
104
+
105
+ (15,8,12);
106
+
107
+ ```
108
+
109
+
110
+
111
+ - 全データ表示
112
+
113
+ ```SQL
114
+
115
+ select id,group_concat(text) as text from id_text
116
+
117
+ group by id;
118
+
119
+ ```
120
+
121
+
122
+
123
+ - 抽出
124
+
125
+ text=1を含むデータの抽出
126
+
127
+ ```SQL
128
+
129
+ select id,group_concat(text) as text from id_text as t1
130
+
131
+ where exists(select 1 from id_text where text=1 and id=t1.id)
132
+
133
+ group by id;
134
+
135
+ ```

2

調整

2019/08/30 07:37

投稿

yambejp
yambejp

スコア114878

test CHANGED
@@ -1,4 +1,4 @@
1
- 正規表現ですから先頭か,から始まり、その数字、,か末尾で終わる
1
+ 正規表現ですから先頭か,から始まり、その数字、,か末尾で終わる
2
2
 
3
3
  と書きます。
4
4
 

1

調整

2019/08/30 07:25

投稿

yambejp
yambejp

スコア114878

test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
 
32
32
 
33
- -1,2,10順番に
33
+ - 1,2,10順番に
34
34
 
35
35
  ```SQL
36
36