回答編集履歴

3

説明文の表現を少し修正

2016/08/12 03:09

投稿

minr
minr

スコア37

answer CHANGED
@@ -1,5 +1,5 @@
1
1
  以下のSQLはいかがでしょうか。
2
- スマートではないもしれませんが、できるだけ簡単な方法を考えてみました。
2
+ 同じような記述が続くの好みかれると思いが、できるだけ簡単で確実な方法を考えてみました。
3
3
 
4
4
  ```SQL
5
5
  select menu_code from table001 where ingredient_code = 'abc'
@@ -12,7 +12,6 @@
12
12
 
13
13
 
14
14
  ちなみに、menu_code列以外も取得する場合は、以下で実現できます。
15
- (サブクエリを使用していますが)
16
15
 
17
16
  ```SQL
18
17
  select * from table001

2

動作確認を行いましたので、動作確認内容とその結果を追記しました。

2016/08/12 03:09

投稿

minr
minr

スコア37

answer CHANGED
@@ -23,4 +23,83 @@
23
23
  intersect
24
24
  select menu_code from table001 where ingredient_code = 'zzz'
25
25
  );
26
- ```
26
+ ```
27
+
28
+ **追記:**
29
+ **以下の通り、動作確認をしてみました。PostgreSQLのバージョンは9.5.3です。**
30
+ **(長くなってしまい申し訳ございません。)**
31
+
32
+ ```SQL
33
+ --テスト用テーブル・データ作成
34
+ drop table if exists table001;
35
+ create table table001(
36
+ menu_code varchar(3),
37
+ ingredient_code varchar(3),
38
+ other_key varchar(3),
39
+ note text,
40
+ --同一のmenu_code,ingredient_codeの組み合わせが複数行存在する場合も想定
41
+ primary key(menu_code,ingredient_code,other_key)
42
+ );
43
+ insert into table001 values('100','abc','A','OK');
44
+ insert into table001 values('100','xxx','B','OK');
45
+ insert into table001 values('100','zzz','C','OK');
46
+ insert into table001 values('101','efg','A','NG');
47
+ insert into table001 values('102','hij','A','NG');
48
+ insert into table001 values('103','abc','A','NG');
49
+ insert into table001 values('104','abc','A','NG');
50
+ insert into table001 values('104','xxx','B','NG');
51
+ insert into table001 values('105','abc','A','NG');
52
+ insert into table001 values('105','abc','B','NG');
53
+ insert into table001 values('105','zzz','C','NG');
54
+ insert into table001 values('106','abc','A','OK');
55
+ insert into table001 values('106','xxx','B','OK');
56
+ insert into table001 values('106','zzz','C','OK');
57
+ insert into table001 values('106','zzz','D','OK');
58
+ insert into table001 values('107','abc','A','OK');
59
+ insert into table001 values('107','xxx','B','OK');
60
+ insert into table001 values('107','zzz','C','OK');
61
+ insert into table001 values('107','efg','D','OK');
62
+
63
+ --テスト1(menu_codeが1行以上該当する場合)
64
+ select * from table001
65
+ where menu_code in(
66
+ select menu_code from table001 where ingredient_code = 'abc'
67
+ intersect
68
+ select menu_code from table001 where ingredient_code = 'xxx'
69
+ intersect
70
+ select menu_code from table001 where ingredient_code = 'zzz'
71
+ );
72
+
73
+ --結果
74
+ -- menu_code | ingredient_code | other_key | note
75
+ -------------+-----------------+-----------+------
76
+ -- 100 | abc | A | OK
77
+ -- 100 | xxx | B | OK
78
+ -- 100 | zzz | C | OK
79
+ -- 106 | abc | A | OK
80
+ -- 106 | xxx | B | OK
81
+ -- 106 | zzz | C | OK
82
+ -- 106 | zzz | D | OK
83
+ -- 107 | abc | A | OK
84
+ -- 107 | xxx | B | OK
85
+ -- 107 | zzz | C | OK
86
+ -- 107 | efg | D | OK
87
+ --(11 行)
88
+
89
+ --テスト2(menu_codeが1行も該当しない場合)
90
+ select * from table001
91
+ where menu_code in(
92
+ select menu_code from table001 where ingredient_code = 'abc'
93
+ intersect
94
+ select menu_code from table001 where ingredient_code = 'xxx'
95
+ intersect
96
+ select menu_code from table001 where ingredient_code = 'fff'
97
+ );
98
+
99
+ --結果
100
+ -- menu_code | ingredient_code | other_key | note
101
+ -------------+-----------------+-----------+------
102
+ --(0 行)
103
+ ```
104
+
105
+

1

スマートではないかもしれませんが、できるだけ簡単な方法を考えてみました。

2016/08/12 02:25

投稿

minr
minr

スコア37

answer CHANGED
@@ -1,4 +1,5 @@
1
1
  以下のSQLはいかがでしょうか。
2
+ スマートではないかもしれませんが、できるだけ簡単な方法を考えてみました。
2
3
 
3
4
  ```SQL
4
5
  select menu_code from table001 where ingredient_code = 'abc'
@@ -10,7 +11,7 @@
10
11
  ```
11
12
 
12
13
 
13
- ちなみに、menu_code列以外も取得する場合は、以下で実現できると思います。
14
+ ちなみに、menu_code列以外も取得する場合は、以下で実現できます。
14
15
  (サブクエリを使用していますが)
15
16
 
16
17
  ```SQL