我想在Postgresql数据库的sql中添加此函数

时间:2015-05-27 16:44:40

标签: sql postgresql function plpgsql

我写过这个函数:

create function check_same_price() returns integer as $result$
declare
 result integer;
begin
 set res = (select *
  from (select count(distinct title), price_dvd_buy
  from dvd_to_buy
  group by price_dvd_buy) as dummy
  where count>1);
if (res==none) then result=0;
else result=1;
end if;
end;
$result$ language plpgsql;

如果相同标题的价格相同,则函数返回0,否则返回1。 完成这项检查是因为我希望同一部电影的价格相同; 我正在使用phppgadmin来管理它。 我收到此错误:

SQL error:

ERROR:  error of sintax to or near "("
LINE 5:  set res = (select *

出了什么问题,我不知道要解决它。谢谢!

1 个答案:

答案 0 :(得分:1)

create function check_same_price()
returns boolean as $$
begin
   return exists(select count(distinct title)
                    from dvd_to_buy
                   group by price_dvd_buy
                   having count(distinct title) > 1)
end;
$$ language plpgsql;