回答編集履歴

3

説明文の表現を少し修正

2016/08/12 03:09

投稿

minr
minr

スコア37

test CHANGED
@@ -1,6 +1,6 @@
1
1
  以下のSQLはいかがでしょうか。
2
2
 
3
- スマートではないもしれませんが、できるだけ簡単な方法を考えてみました。
3
+ 同じような記述が続くの好みかれると思いが、できるだけ簡単で確実な方法を考えてみました。
4
4
 
5
5
 
6
6
 
@@ -26,8 +26,6 @@
26
26
 
27
27
  ちなみに、menu_code列以外も取得する場合は、以下で実現できます。
28
28
 
29
- (サブクエリを使用していますが)
30
-
31
29
 
32
30
 
33
31
  ```SQL

2

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

2016/08/12 03:09

投稿

minr
minr

スコア37

test CHANGED
@@ -49,3 +49,163 @@
49
49
  );
50
50
 
51
51
  ```
52
+
53
+
54
+
55
+ **追記:**
56
+
57
+ **以下の通り、動作確認をしてみました。PostgreSQLのバージョンは9.5.3です。**
58
+
59
+ **(長くなってしまい申し訳ございません。)**
60
+
61
+
62
+
63
+ ```SQL
64
+
65
+ --テスト用テーブル・データ作成
66
+
67
+ drop table if exists table001;
68
+
69
+ create table table001(
70
+
71
+ menu_code varchar(3),
72
+
73
+ ingredient_code varchar(3),
74
+
75
+ other_key varchar(3),
76
+
77
+ note text,
78
+
79
+ --同一のmenu_code,ingredient_codeの組み合わせが複数行存在する場合も想定
80
+
81
+ primary key(menu_code,ingredient_code,other_key)
82
+
83
+ );
84
+
85
+ insert into table001 values('100','abc','A','OK');
86
+
87
+ insert into table001 values('100','xxx','B','OK');
88
+
89
+ insert into table001 values('100','zzz','C','OK');
90
+
91
+ insert into table001 values('101','efg','A','NG');
92
+
93
+ insert into table001 values('102','hij','A','NG');
94
+
95
+ insert into table001 values('103','abc','A','NG');
96
+
97
+ insert into table001 values('104','abc','A','NG');
98
+
99
+ insert into table001 values('104','xxx','B','NG');
100
+
101
+ insert into table001 values('105','abc','A','NG');
102
+
103
+ insert into table001 values('105','abc','B','NG');
104
+
105
+ insert into table001 values('105','zzz','C','NG');
106
+
107
+ insert into table001 values('106','abc','A','OK');
108
+
109
+ insert into table001 values('106','xxx','B','OK');
110
+
111
+ insert into table001 values('106','zzz','C','OK');
112
+
113
+ insert into table001 values('106','zzz','D','OK');
114
+
115
+ insert into table001 values('107','abc','A','OK');
116
+
117
+ insert into table001 values('107','xxx','B','OK');
118
+
119
+ insert into table001 values('107','zzz','C','OK');
120
+
121
+ insert into table001 values('107','efg','D','OK');
122
+
123
+
124
+
125
+ --テスト1(menu_codeが1行以上該当する場合)
126
+
127
+ select * from table001
128
+
129
+ where menu_code in(
130
+
131
+ select menu_code from table001 where ingredient_code = 'abc'
132
+
133
+ intersect
134
+
135
+ select menu_code from table001 where ingredient_code = 'xxx'
136
+
137
+ intersect
138
+
139
+ select menu_code from table001 where ingredient_code = 'zzz'
140
+
141
+ );
142
+
143
+
144
+
145
+ --結果
146
+
147
+ -- menu_code | ingredient_code | other_key | note
148
+
149
+ -------------+-----------------+-----------+------
150
+
151
+ -- 100 | abc | A | OK
152
+
153
+ -- 100 | xxx | B | OK
154
+
155
+ -- 100 | zzz | C | OK
156
+
157
+ -- 106 | abc | A | OK
158
+
159
+ -- 106 | xxx | B | OK
160
+
161
+ -- 106 | zzz | C | OK
162
+
163
+ -- 106 | zzz | D | OK
164
+
165
+ -- 107 | abc | A | OK
166
+
167
+ -- 107 | xxx | B | OK
168
+
169
+ -- 107 | zzz | C | OK
170
+
171
+ -- 107 | efg | D | OK
172
+
173
+ --(11 行)
174
+
175
+
176
+
177
+ --テスト2(menu_codeが1行も該当しない場合)
178
+
179
+ select * from table001
180
+
181
+ where menu_code in(
182
+
183
+ select menu_code from table001 where ingredient_code = 'abc'
184
+
185
+ intersect
186
+
187
+ select menu_code from table001 where ingredient_code = 'xxx'
188
+
189
+ intersect
190
+
191
+ select menu_code from table001 where ingredient_code = 'fff'
192
+
193
+ );
194
+
195
+
196
+
197
+ --結果
198
+
199
+ -- menu_code | ingredient_code | other_key | note
200
+
201
+ -------------+-----------------+-----------+------
202
+
203
+ --(0 行)
204
+
205
+ ```
206
+
207
+
208
+
209
+
210
+
211
+

1

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

2016/08/12 02:25

投稿

minr
minr

スコア37

test CHANGED
@@ -1,4 +1,6 @@
1
1
  以下のSQLはいかがでしょうか。
2
+
3
+ スマートではないかもしれませんが、できるだけ簡単な方法を考えてみました。
2
4
 
3
5
 
4
6
 
@@ -22,7 +24,7 @@
22
24
 
23
25
 
24
26
 
25
- ちなみに、menu_code列以外も取得する場合は、以下で実現できると思います。
27
+ ちなみに、menu_code列以外も取得する場合は、以下で実現できます。
26
28
 
27
29
  (サブクエリを使用していますが)
28
30