副問い合わせについて
SQL
1 2create table asobi_a(name varchar(50),flag int,jotai varchar(2),sex int,age int); 3insert into asobi_a values("a",0,"00","0",21); 4insert into asobi_a values("b",0,"00","1",18); 5insert into asobi_a values("c",0,"00","1",15); 6insert into asobi_a values("d",0,"00","0",21); 7insert into asobi_a values("e",0,"00","1",42); 8insert into asobi_a values("f",0,"00","0",34); 9insert into asobi_a values("g",0,"00","0",76); 10insert into asobi_a values("h",0,"00","0",18); 11insert into asobi_a values("i",0,"00","1",20); 12insert into asobi_a values("j",0,"00","1",35); 13insert into asobi_a values("k",0,"00","1",16); 14insert into asobi_a values("l",0,"00","0",26); 15insert into asobi_a values("m",0,"00","0",53); 16insert into asobi_a values("n",0,"00","0",21); 17insert into asobi_a values("o",0,"00","1",32); 18+------+------+-------+------+------+ 19| name | flag | jotai | sex | age | 20+------+------+-------+------+------+ 21| a | 0 | 00 | 0 | 21 | 22| b | 0 | 00 | 1 | 18 | 23| c | 0 | 00 | 1 | 15 | 24| d | 0 | 00 | 0 | 21 | 25| e | 0 | 00 | 1 | 42 | 26| f | 0 | 00 | 0 | 34 | 27| g | 0 | 00 | 0 | 76 | 28| h | 0 | 00 | 0 | 18 | 29| i | 0 | 00 | 1 | 20 | 30| j | 0 | 00 | 1 | 35 | 31| k | 0 | 00 | 1 | 16 | 32| l | 0 | 00 | 0 | 26 | 33| m | 0 | 00 | 0 | 53 | 34| n | 0 | 00 | 0 | 21 | 35| o | 0 | 00 | 1 | 32 | 36+------+------+-------+------+------+ 37create table asobi_b(bl char(2),name varchar(50)); 38insert into asobi_b values("a","a"); 39insert into asobi_b values("o","b"); 40insert into asobi_b values("ab","c"); 41insert into asobi_b values("a","d"); 42insert into asobi_b values("b","e"); 43insert into asobi_b values("b","f"); 44insert into asobi_b values("b","g"); 45insert into asobi_b values("o","h"); 46insert into asobi_b values("a","i"); 47insert into asobi_b values("a","j"); 48insert into asobi_b values("a","k"); 49insert into asobi_b values("o","l"); 50insert into asobi_b values("a","m"); 51insert into asobi_b values("b","n"); 52insert into asobi_b values("ab","o"); 53+------+------+ 54| bl | name | 55+------+------+ 56| a | a | 57| o | b | 58| ab | c | 59| a | d | 60| b | e | 61| b | f | 62| b | g | 63| o | h | 64| a | i | 65| a | j | 66| a | k | 67| o | l | 68| a | m | 69| b | n | 70| ab | o | 71+------+------+ 72 73select 74 sex,count(*) 75from 76 asobi_a 77where 78 age IN 79( 80select 81 sex,avg(AGE) 82from 83 asobi_a,asobi_b 84where 85 ((asobi_b.bl = "a" and asobi_a.sex = "0") 86 or (asobi_b.bl = "o" and asobi_a.sex = "1")) 87 and asobi_b.bl in("a","o") 88 and (asobi_a.name = asobi_b.name 89 and asobi_a.FLAG = 0 and asobi_a.JOTAI = "00") 90group by 91 sex 92) 93group by 94 sex; 95
副問い合わせの中は大まかに言えば血液型が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 や sex = "0" などとりあえず入れてみたものの
ERROR 1241 (21000): Operand should contain 1 column(s)
と出てしまいます。
エラーが出る原因も何をすればエラーが出ないのかも分かりません。ぜひわかれば教えて頂きたいです。
バージョンは8.0です。
回答3件
あなたの回答
tips
プレビュー