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

回答編集履歴

1

質問に書かれたテーブルに従ってSQLを修正しました。

2020/07/01 13:34

投稿

etsuhisa
etsuhisa

スコア416

answer CHANGED
@@ -1,5 +1,7 @@
1
+ 項目名はすべて小文字なのですね。
2
+ また、男性は男性の平均年齢なのでasobi_a.sex = '1'ですね。
1
- 実際に動かして検証していませんが、質問の内容からすると以下のような条件になるのではなでしょうか?
3
+ 質問にあデータだ一致するレコードはないですが、以下のようになると思ます。
2
- ちなみに、男性の一致する年齢も女性の平均年齢でよいのでょうか?
4
+ 女性の血液型をB型にすれば1件ヒットます。)
3
5
 
4
6
  ```SQL
5
7
  select
@@ -7,11 +9,13 @@
7
9
  from
8
10
  asobi_a,asobi_b
9
11
  where
10
- ((asobi_b.bl = 'a' and asobi_a.sex = '0')
12
+ (asobi_b.bl = 'a' and asobi_a.sex = '0'
13
+ and asobi_a.age = (select round(avg(age)) from asobi_a where asobi_a.sex = '0')
14
+ or
11
- or (asobi_b.bl = 'o' and asobi_a.sex = '1'))
15
+ asobi_b.bl = 'o' and asobi_a.sex = '1'
16
+ and asobi_a.age = (select round(avg(age)) from asobi_a where asobi_a.sex = '1'))
12
- and (asobi_a.name = asobi_b.name
17
+ and asobi_a.name = asobi_b.name
13
- and asobi_a.FLAG = 0 and asobi_b.JOTAI = '00')
18
+ and asobi_a.flag = 0 and asobi_a.jotai = '00'
14
- and asobi_a.AGE = (select round(avg(AGE)) from asobi_a where asobi_a.sex = '0')
15
19
  group by
16
- sex
20
+ sex;
17
21
  ```