错误代码:1172,结果在mysql中包含多行

nlejzf6q  于 2022-12-17  发布在  Mysql
关注(0)|答案(1)|浏览(319)

我的代码返回错误错误代码:1172.当我运行下面的代码时,结果包含了不止一行:

delimiter //
create function purchase_status(customernumber int) returns varchar(100) reads sql data
begin
    declare purchase_state varchar(100);
    select 
        case
            when amount < 25000 then "Silver" 
            when amount between 25000 and 50000 then "Gold"
            else "Platinum"
        end as purchase_state2 into purchase_state
    from payments;
    return purchase_status;
end//

#drop function purchase_status;

select customernumber,customername,purchase_status(customernumber) as purchase_status
from customers;

我不知道该怎么称呼它。

wqsoz72f

wqsoz72f1#

如果使用**into purchase_state**,则函数内的Select子句必须返回一行。
您可能忘记了customernumberSUM(amount)上的WHERE子句,而不是amount。
示例:

select 
  case
    when SUM(amount) < 25000 then "Silver" 
    when SUM(amount) between 25000 and 50000 then "Gold"
    else "Platinum"
  end as purchase_state2 into purchase_state
from payments WHERE customernumber = customernumber

相关问题