本の主題に基づいて本の価格の平均を与える関数を作成する必要があります。この関数のルールは次のとおりです。
a) 引数が null の場合、null を返す
b) 引数がトピック テーブルにあるトピック ID と一致しない場合は、-2 の値を返します。
c) 引数がトピック テーブルにあるトピック ID と一致するが、そのトピックの本がない場合は、-1 の値を返します。
create function AvgPriceByTopic(
p_subject varchar(20))
RETURNS decimal(8,2)
begin
declare v_avgPrice decimal(8,2);
declare v_avgListPrice decimal(8,2);
if p_subject is null then
set v_avgPrice := null;
elseif exists (
select avg(list_price) into v_avgListPrice
from books
where topic_id = p_subject
group by book_id
limit 1 ) then
set v_avgPrice := v_avgListPrice;
else
set v_avgPrice := -2;
end if;
return v_avgPrice;
end;
#
次のようなエラーが表示されます。
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'into v_avgListPrice from books' at line 11
このエラーを取り除くための提案はありますか? 構文に問題がある場合があります... よろしくお願いします。