質問編集履歴

12

2018/06/28 23:46

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -232,4 +232,4 @@
232
232
 
233
233
 
234
234
 
235
- というものになります。
235
+ というものになります。回答者様のコメント欄にも詳しく記載してますので併せてご覧下さい。

11

2018/06/28 23:46

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -211,3 +211,25 @@
211
211
  ALTER TABLE `store_mst` ADD UNIQUE(`store_id`);
212
212
 
213
213
  ```
214
+
215
+
216
+
217
+
218
+
219
+ ---
220
+
221
+
222
+
223
+ 補足2
224
+
225
+
226
+
227
+ 伝わりづらいようなので要件の説明を追加します。
228
+
229
+
230
+
231
+ チェーン店で設定した商品分類タグはそれに紐づく店舗でも共通して画面表示されるので、チェーン店の共通タグと店舗個別のタグを足した合計が5件を超えるstore_idを取得したい
232
+
233
+
234
+
235
+ というものになります。

10

2018/06/28 23:45

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -204,7 +204,7 @@
204
204
 
205
205
 
206
206
 
207
- INSERT INTO `store_mst` (`store_id`, `groupstore_id`, `store_name`) VALUES ('101', '201', 'テスト店舗');
207
+ INSERT INTO `store_mst` (`store_id`, `groupstore_id`, `store_name`) VALUES ('101', '201', 'name');
208
208
 
209
209
 
210
210
 

9

2018/06/28 01:31

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -68,7 +68,9 @@
68
68
 
69
69
  SELECT
70
70
 
71
- T1.store_id(
71
+ T1.store_id,
72
+
73
+ (
72
74
 
73
75
  SELECT COUNT(*)
74
76
 

8

2018/06/28 01:22

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -202,7 +202,7 @@
202
202
 
203
203
 
204
204
 
205
- INSERT INTO `store_mst` (`store_id`, `groupstore_id`, `store_name`) VALUES ('101', '201', 'テスト店舗')
205
+ INSERT INTO `store_mst` (`store_id`, `groupstore_id`, `store_name`) VALUES ('101', '201', 'テスト店舗');
206
206
 
207
207
 
208
208
 

7

2018/06/28 01:20

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
  SELECT
62
62
 
63
-  T5.store_id
63
+ T5.store_id
64
64
 
65
65
  FROM
66
66
 

6

2018/06/27 23:23

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -156,7 +156,7 @@
156
156
 
157
157
  `groupstore_id` int(11) DEFAULT NULL,
158
158
 
159
- `display_flg` int(11) DEFAULT '0'
159
+ `display_flg` int(11) DEFAULT 0
160
160
 
161
161
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
162
162
 

5

a

2018/06/27 23:22

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -204,4 +204,8 @@
204
204
 
205
205
  INSERT INTO `store_mst` (`store_id`, `groupstore_id`, `store_name`) VALUES ('101', '201', 'テスト店舗');
206
206
 
207
+
208
+
209
+ ALTER TABLE `store_mst` ADD UNIQUE(`store_id`);
210
+
207
- ```
211
+ ```

4

a

2018/06/27 22:41

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  要件は以下です。
14
14
 
15
- 「ログインユーザーが所属する店舗(store_mst)そのチェーン店(groupstore_mst)に紐づく商品分類タグ(tag_mst)で、その商品分類タグのWEB表示(tag_mst.display_flg = 1)件数が合わせて5件を超える店舗IDを取得する」
15
+ 「ログインユーザーが所属する店舗(store_mst)あるいは、そのチェーン店(groupstore_mst)に紐づく商品分類タグ(tag_mst)で、その商品分類タグのWEB表示(tag_mst.display_flg = 1)件数が合わせて5件を超える店舗IDを取得する」
16
16
 
17
17
 
18
18
 

3

a

2018/06/27 16:37

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -118,7 +118,7 @@
118
118
 
119
119
  WHERE
120
120
 
121
- store_id =  101
121
+ store_id = /*storeId*/101
122
122
 
123
123
  GROUP BY
124
124
 

2

a

2018/06/27 16:01

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -58,79 +58,75 @@
58
58
 
59
59
  ```SQL
60
60
 
61
- SELECT
61
+ SELECT
62
-
62
+
63
-  T5.store_id
63
+  T5.store_id
64
-
64
+
65
- FROM
65
+ FROM
66
66
 
67
67
  (
68
68
 
69
- SELECT
69
+ SELECT
70
+
70
-
71
+ T1.store_id(
72
+
71
- COUNT(*)
73
+ SELECT COUNT(*)
72
-
74
+
73
- FROM
75
+ FROM
74
-
76
+
75
- tag_mst T2
77
+ tag_mst T2
76
-
78
+
77
- WHERE
79
+ WHERE
78
-
80
+
79
- T2.store_id = T1.store_id
81
+ T2.store_id = T1.store_id AND T2.display_flg = 1
80
-
81
- AND T2.display_flg = 1
82
+
82
-
83
- GROUP BY
83
+ GROUP BY
84
84
 
85
85
  T2.store_id
86
86
 
87
- ) + (
87
+ ) +(
88
+
88
-
89
+ SELECT COUNT(*)
90
+
91
+ FROM
92
+
93
+ tag_mst T3
94
+
95
+ WHERE
96
+
97
+ T3.groupstore_id =(
98
+
89
- SELECT
99
+ SELECT
90
-
100
+
91
- COUNT(*)
101
+ T4.groupstore_id
92
-
102
+
93
- FROM
103
+ FROM
94
-
104
+
95
- tag_mst T3
105
+ store_mst T4
96
-
106
+
97
- WHERE
107
+ WHERE
98
-
99
- T3.groupstore_id = (
108
+
100
-
101
- SELECT
102
-
103
- T4.groupstore_id
109
+ T4.store_id = T1.store_id
110
+
104
-
111
+ ) AND T3.display_flg = 1
112
+
113
+ ) AS cnt
114
+
105
- FROM
115
+ FROM
106
-
116
+
107
- store_mst T4
117
+ tag_mst T1
108
-
118
+
109
- WHERE
119
+ WHERE
110
-
120
+
111
- T4.store_id = T1.store_id
121
+ store_id =  101
112
-
113
- )
122
+
114
-
115
- AND T3.display_flg = 1
116
-
117
- ) AS cnt
118
-
119
- FROM
120
-
121
- tag_mst T1
122
-
123
- WHERE
124
-
125
- store_id = /*storeId*/ 5100
126
-
127
- GROUP BY
123
+ GROUP BY
128
124
 
129
125
  T1.store_id
130
126
 
131
- )T5
127
+ ) T5
132
-
128
+
133
- WHERE
129
+ WHERE
134
130
 
135
131
  cnt > 5
136
132
 
@@ -143,3 +139,69 @@
143
139
  もう少し良い書き方がしたいのですが、アドバイス頂けないでしょうか?
144
140
 
145
141
  よろしくお願い致します。
142
+
143
+
144
+
145
+ ---
146
+
147
+ 補足
148
+
149
+ ```sql
150
+
151
+ CREATE TABLE `tag_mst` (
152
+
153
+ `tag_id` int(11) NOT NULL,
154
+
155
+ `store_id` int(11) DEFAULT NULL,
156
+
157
+ `groupstore_id` int(11) DEFAULT NULL,
158
+
159
+ `display_flg` int(11) DEFAULT '0'
160
+
161
+ ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
162
+
163
+
164
+
165
+ CREATE TABLE `store_mst` (
166
+
167
+ `store_id` int(11) NOT NULL,
168
+
169
+ `groupstore_id` int(11) DEFAULT NULL,
170
+
171
+ `store_name` text
172
+
173
+ ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
174
+
175
+
176
+
177
+
178
+
179
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('1', '101', NULL, '1');
180
+
181
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('2', '101', NULL, '1');
182
+
183
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('3', '101', NULL, '1');
184
+
185
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('4', '101', NULL, '1');
186
+
187
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('5', '101', NULL, '1');
188
+
189
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('6', '101', NULL, '1');
190
+
191
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('11', NULL, '201', '1');
192
+
193
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('12', NULL, '201', '1');
194
+
195
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('13', NULL, '201', '1');
196
+
197
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('14', NULL, '201', '1');
198
+
199
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('15', NULL, '201', '1');
200
+
201
+ INSERT INTO `tag_mst` (`tag_id`, `store_id`, `groupstore_id`, `display_flg`) VALUES ('16', NULL, '201', '1');
202
+
203
+
204
+
205
+ INSERT INTO `store_mst` (`store_id`, `groupstore_id`, `store_name`) VALUES ('101', '201', 'テスト店舗');
206
+
207
+ ```

1

s

2018/06/27 15:57

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
File without changes