我想创建一个mysql函数,它返回表的第一个“X”百分位数。我尝试了下面给出的并且收到错误..
任何想法/建议请...
create function precent_return (
table_name varchar(50),
percentiles float
) returns int
return (count(*)*percentiles/100 from table_name)
答案 0 :(得分:0)
您需要使用动态SQL:
create function precent_return (
in_table_name varchar(50),
in_percentiles float
) returns int
begin
set @retval = -1;
set @sql = 'select count(*) * @percentiles/100 into @retval from @table_name';
set @sql = replace(@sql, '@percentiles', in_percentiles);
set @sql = replace(@sql, '@table_name', in_table_name);
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
return(@retval);
end;