質問編集履歴

7

コードの編集

2020/07/01 12:54

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,145 @@
4
4
 
5
5
 
6
6
 
7
-
7
+ create table asobi_a(name varchar(50),flag int,jotai varchar(2),sex int,age int);
8
+
9
+ insert into asobi_a values("a",0,"00","0",21);
10
+
11
+ insert into asobi_a values("b",0,"00","1",18);
12
+
13
+ insert into asobi_a values("c",0,"00","1",15);
14
+
15
+ insert into asobi_a values("d",0,"00","0",21);
16
+
17
+ insert into asobi_a values("e",0,"00","1",42);
18
+
19
+ insert into asobi_a values("f",0,"00","0",34);
20
+
21
+ insert into asobi_a values("g",0,"00","0",76);
22
+
23
+ insert into asobi_a values("h",0,"00","0",18);
24
+
25
+ insert into asobi_a values("i",0,"00","1",20);
26
+
27
+ insert into asobi_a values("j",0,"00","1",35);
28
+
29
+ insert into asobi_a values("k",0,"00","1",16);
30
+
31
+ insert into asobi_a values("l",0,"00","0",26);
32
+
33
+ insert into asobi_a values("m",0,"00","0",53);
34
+
35
+ insert into asobi_a values("n",0,"00","0",21);
36
+
37
+ insert into asobi_a values("o",0,"00","1",32);
38
+
39
+ +------+------+-------+------+------+
40
+
41
+ | name | flag | jotai | sex | age |
42
+
43
+ +------+------+-------+------+------+
44
+
45
+ | a | 0 | 00 | 0 | 21 |
46
+
47
+ | b | 0 | 00 | 1 | 18 |
48
+
49
+ | c | 0 | 00 | 1 | 15 |
50
+
51
+ | d | 0 | 00 | 0 | 21 |
52
+
53
+ | e | 0 | 00 | 1 | 42 |
54
+
55
+ | f | 0 | 00 | 0 | 34 |
56
+
57
+ | g | 0 | 00 | 0 | 76 |
58
+
59
+ | h | 0 | 00 | 0 | 18 |
60
+
61
+ | i | 0 | 00 | 1 | 20 |
62
+
63
+ | j | 0 | 00 | 1 | 35 |
64
+
65
+ | k | 0 | 00 | 1 | 16 |
66
+
67
+ | l | 0 | 00 | 0 | 26 |
68
+
69
+ | m | 0 | 00 | 0 | 53 |
70
+
71
+ | n | 0 | 00 | 0 | 21 |
72
+
73
+ | o | 0 | 00 | 1 | 32 |
74
+
75
+ +------+------+-------+------+------+
76
+
77
+ create table asobi_b(bl char(2),name varchar(50));
78
+
79
+ insert into asobi_b values("a","a");
80
+
81
+ insert into asobi_b values("o","b");
82
+
83
+ insert into asobi_b values("ab","c");
84
+
85
+ insert into asobi_b values("a","d");
86
+
87
+ insert into asobi_b values("b","e");
88
+
89
+ insert into asobi_b values("b","f");
90
+
91
+ insert into asobi_b values("b","g");
92
+
93
+ insert into asobi_b values("o","h");
94
+
95
+ insert into asobi_b values("a","i");
96
+
97
+ insert into asobi_b values("a","j");
98
+
99
+ insert into asobi_b values("a","k");
100
+
101
+ insert into asobi_b values("o","l");
102
+
103
+ insert into asobi_b values("a","m");
104
+
105
+ insert into asobi_b values("b","n");
106
+
107
+ insert into asobi_b values("ab","o");
108
+
109
+ +------+------+
110
+
111
+ | bl | name |
112
+
113
+ +------+------+
114
+
115
+ | a | a |
116
+
117
+ | o | b |
118
+
119
+ | ab | c |
120
+
121
+ | a | d |
122
+
123
+ | b | e |
124
+
125
+ | b | f |
126
+
127
+ | b | g |
128
+
129
+ | o | h |
130
+
131
+ | a | i |
132
+
133
+ | a | j |
134
+
135
+ | a | k |
136
+
137
+ | o | l |
138
+
139
+ | a | m |
140
+
141
+ | b | n |
142
+
143
+ | ab | o |
144
+
145
+ +------+------+
8
146
 
9
147
 
10
148
 
@@ -18,37 +156,37 @@
18
156
 
19
157
  where
20
158
 
21
-
159
+ age IN
22
160
 
23
161
  (
24
162
 
25
163
  select
26
164
 
27
- sex,avg(AGE)
165
+ sex,avg(AGE)
28
166
 
29
167
  from
30
168
 
31
- asobi_a,asobi_b
169
+ asobi_a,asobi_b
32
170
 
33
171
  where
34
172
 
35
- ((asobi_b.bl = "a" and asobi_a.sex = "0")
173
+ ((asobi_b.bl = "a" and asobi_a.sex = "0")
36
-
174
+
37
- or (asobi_b.bl = "o" and asobi_a.sex = "1"))
175
+ or (asobi_b.bl = "o" and asobi_a.sex = "1"))
38
-
176
+
39
- and asobi_a.bl in("a","o")
177
+ and asobi_b.bl in("a","o")
40
-
178
+
41
- and (asobi_a.name = asobi_b.name
179
+ and (asobi_a.name = asobi_b.name
42
-
180
+
43
- and asobi_a.FLAG = 0 and asobi_b.JOTAI = "00")
181
+ and asobi_a.FLAG = 0 and asobi_a.JOTAI = "00")
44
182
 
45
183
  group by
46
184
 
47
- sex
185
+ sex
48
186
 
49
187
  )
50
188
 
51
- group by
189
+ group by
52
190
 
53
191
  sex;
54
192
 
@@ -58,7 +196,21 @@
58
196
 
59
197
 
60
198
 
61
- 副問い合わせの中は大まかに言えば血液型がaかつ、男性か、血液型がoかつ、女性の人の平均年齢を性別ごとに求めています。そして、副問い合わせが(asobi_b.bl = "a" and asobi_a.sex = "0")の場合asobi_a.sex = "0"の平均年齢と一致する人を、(asobi_b.bl = "o" and asobi_a.sex = "1")の場合asobi_a.sex = "0"の平均年齢と一致する人を出力したいのですが、今のSQL文だと、where句の中にage inと書いても、ERROR1241と出てしまいます。
199
+ 副問い合わせの中は大まかに言えば血液型がaかつ、男性か、血液型がoかつ、女性の人の平均年齢を性別ごとに求めています。
200
+
201
+
202
+
203
+ そして、副問い合わせが
204
+
205
+ (asobi_b.bl = "a" and asobi_a.sex = "0")の場合asobi_a.sex = "0"の平均年齢と一致する人を、(asobi_b.bl = "o" and asobi_a.sex = "1")の場合asobi_a.sex = "0"の平均年齢と一致する人を
206
+
207
+ 出力したいのですが、今のSQL文だと、where句の中に、age in や sex = "0" などとりあえず入れてみたものの
208
+
209
+ ERROR 1241 (21000): Operand should contain 1 column(s)
210
+
211
+ と出てしまいます。
212
+
213
+
62
214
 
63
215
  エラーが出る原因も何をすればエラーが出ないのかも分かりません。ぜひわかれば教えて頂きたいです。
64
216
 

6

2020/07/01 12:53

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,12 @@
1
1
  ### 副問い合わせについて
2
2
 
3
3
  ```SQL
4
+
5
+
6
+
7
+
8
+
9
+
4
10
 
5
11
  select
6
12
 

5

2020/07/01 03:03

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,10 +1,6 @@
1
1
  ### 副問い合わせについて
2
2
 
3
3
  ```SQL
4
-
5
- コード
6
-
7
- ```
8
4
 
9
5
  select
10
6
 
@@ -52,6 +48,8 @@
52
48
 
53
49
 
54
50
 
51
+ ```
52
+
55
53
 
56
54
 
57
55
  副問い合わせの中は大まかに言えば血液型がaかつ、男性か、血液型がoかつ、女性の人の平均年齢を性別ごとに求めています。そして、副問い合わせが(asobi_b.bl = "a" and asobi_a.sex = "0")の場合asobi_a.sex = "0"の平均年齢と一致する人を、(asobi_b.bl = "o" and asobi_a.sex = "1")の場合asobi_a.sex = "0"の平均年齢と一致する人を出力したいのですが、今のSQL文だと、where句の中にage inと書いても、ERROR1241と出てしまいます。

4

2020/07/01 02:21

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,10 @@
1
1
  ### 副問い合わせについて
2
+
3
+ ```SQL
4
+
5
+ コード
6
+
7
+ ```
2
8
 
3
9
  select
4
10
 
@@ -59,13 +65,3 @@
59
65
 
60
66
 
61
67
  バージョンは8.0です。
62
-
63
-
64
-
65
-
66
-
67
- ```SQL
68
-
69
- コード
70
-
71
- ```

3

2020/07/01 02:21

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -64,8 +64,8 @@
64
64
 
65
65
 
66
66
 
67
- ### 該当のソースコード
67
+ ```SQL
68
68
 
69
+ コード
69
70
 
70
-
71
- MySQL
71
+ ```

2

2020/07/01 02:19

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,9 @@
48
48
 
49
49
 
50
50
 
51
- 副問い合わせが(asobi_b.bl = "a" and asobi_a.sex = "0")の場合asobi_a.sex = "0"の平均年齢と一致する人を、(asobi_b.bl = "o" and asobi_a.sex = "1")の場合asobi_a.sex = "0"の平均年齢と一致する人を出力したいが、どうやって特定条件検索るかわからなす。
51
+ 副問い合わせの中は大まかに言えば血液型aかつ、男性か、血液型がoかつ、女性の人の平均年齢を性別ごとに求めています。そして、副問い合わせが(asobi_b.bl = "a" and asobi_a.sex = "0")の場合asobi_a.sex = "0"の平均年齢と一致する人を、(asobi_b.bl = "o" and asobi_a.sex = "1")の場合asobi_a.sex = "0"の平均年齢と一致する人を出力したいのですが、今のSQL文だと、where句の中にage inと書ても、ERROR1241と出てしまいます。
52
+
53
+ エラーが出る原因も何をすればエラーが出ないのかも分かりません。ぜひわかれば教えて頂きたいです。
52
54
 
53
55
 
54
56
 

1

2020/07/01 02:19

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -54,6 +54,14 @@
54
54
 
55
55
 
56
56
 
57
+
58
+
59
+ バージョンは8.0です。
60
+
61
+
62
+
63
+
64
+
57
65
  ### 該当のソースコード
58
66
 
59
67