回答編集履歴

3

「含む」の解釈の明確化

2021/07/08 06:23

投稿

hoshi-takanori
hoshi-takanori

スコア7895

test CHANGED
@@ -1,10 +1,10 @@
1
- 「"aaa"と"zzz"のみ含んだuser_id」の解釈が微妙ですが、aaa と zzz の両方を含み、それ以外 (bbb や ccc など) を含まないということなら、こんな感じでしょうか。
1
+ 「"aaa"と"zzz"のみ含んだuser_id」の解釈が微妙ですが、aaa を含む code と zzz を含む code の両方を持ち、それ以外 code (bbb や ccc など) を持たない user_id」ということなら、こんな感じでしょうか。
2
2
 
3
3
 
4
4
 
5
5
  ```sql
6
6
 
7
- create table "table" (id int primary key, user_id int, code varchar(10));
7
+ create table "table" (id int primary key, user_id int, code text);
8
8
 
9
9
 
10
10
 

2

追記

2021/07/08 06:23

投稿

hoshi-takanori
hoshi-takanori

スコア7895

test CHANGED
@@ -65,3 +65,27 @@
65
65
  insert into "table" values (14, 16, 'aaazzz');
66
66
 
67
67
  ```
68
+
69
+
70
+
71
+ ---
72
+
73
+
74
+
75
+ 追記。intersect と except でもいけますね。
76
+
77
+
78
+
79
+ ```sql
80
+
81
+ (select user_id from "table" where code like '%aaa%'
82
+
83
+ intersect
84
+
85
+ select user_id from "table" where code like '%zzz%')
86
+
87
+ except
88
+
89
+ select user_id from "table" where code not like '%aaa%' and code not like '%zzz%';
90
+
91
+ ```

1

セミコロン追加

2021/07/08 06:03

投稿

hoshi-takanori
hoshi-takanori

スコア7895

test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
  and user_id in (select user_id from "table" where code like '%zzz%')
52
52
 
53
- and user_id not in (select user_id from "table" where code not like '%aaa%' and code not like '%zzz%')
53
+ and user_id not in (select user_id from "table" where code not like '%aaa%' and code not like '%zzz%');
54
54
 
55
55
  ```
56
56