質問するログイン新規登録

回答編集履歴

3

「含む」の解釈の明確化

2021/07/08 06:23

投稿

hoshi-takanori
hoshi-takanori

スコア7903

answer CHANGED
@@ -1,7 +1,7 @@
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
  ```sql
4
- create table "table" (id int primary key, user_id int, code varchar(10));
4
+ create table "table" (id int primary key, user_id int, code text);
5
5
 
6
6
  insert into "table" values (1, 10, 'aaa1');
7
7
  insert into "table" values (2, 10, 'bbb1');

2

追記

2021/07/08 06:23

投稿

hoshi-takanori
hoshi-takanori

スコア7903

answer CHANGED
@@ -31,4 +31,16 @@
31
31
 
32
32
  ```sql
33
33
  insert into "table" values (14, 16, 'aaazzz');
34
+ ```
35
+
36
+ ---
37
+
38
+ 追記。intersect と except でもいけますね。
39
+
40
+ ```sql
41
+ (select user_id from "table" where code like '%aaa%'
42
+ intersect
43
+ select user_id from "table" where code like '%zzz%')
44
+ except
45
+ select user_id from "table" where code not like '%aaa%' and code not like '%zzz%';
34
46
  ```

1

セミコロン追加

2021/07/08 06:03

投稿

hoshi-takanori
hoshi-takanori

スコア7903

answer CHANGED
@@ -24,7 +24,7 @@
24
24
  from "table"
25
25
  where user_id in (select user_id from "table" where code like '%aaa%')
26
26
  and user_id in (select user_id from "table" where code like '%zzz%')
27
- and user_id not in (select user_id from "table" where code not like '%aaa%' and code not like '%zzz%')
27
+ and user_id not in (select user_id from "table" where code not like '%aaa%' and code not like '%zzz%');
28
28
  ```
29
29
 
30
30
  なお、aaazzz があるとそれも引っかかりますね…。