MYSQL:错误1241(21000):操作数应包含1列

MYSQL:错误1241(21000):操作数应包含1列,mysql,sql,Mysql,Sql,这应该是工作,但我得到了一个错误不知何故。我想从表中得到credits列,然后乘以100。问题是获得给定学生id和年份的学分数,并获得总付款。假设每个信用卡是100美元 delimiter // create function fTest (stuYear varchar(4), stuID varchar(4)) returns varchar(200) begin declare msg varchar(200) default ''; if (stuYear = '' or stuYear

这应该是工作,但我得到了一个错误不知何故。我想从表中得到credits列,然后乘以100。问题是获得给定学生id和年份的学分数,并获得总付款。假设每个信用卡是100美元

delimiter //
create function fTest (stuYear varchar(4), stuID varchar(4))
returns varchar(200)
begin
declare msg varchar(200) default '';
if (stuYear = '' or stuYear is null) then 
    select 'Please input a valid year' into msg;
elseif (stuID = '' or stuID is null) then 
    select 'Please input a student  id' into msg;    
else
begin

if (msg = '' or msg is null) then
    select ('No result found for student ID: ', stuID, ' at year: ', stuYear) into msg; 
select (credits * 100) into msg from Students_Courses natural join Courses where sid=stuID and year=stuYear group by credits;
return msg ;    
end if;    
end ;
end if;
end ;
//
delimiter ;
这是不正确的:

select ('No result found for student ID: ', stuID, ' at year: ', stuYear)
select语句可以包含多列,但不应将它们括在括号中。此外,这将返回多个值,您无法在单个消息中选择这些值

我猜你想把这些值合并成一个值。您可以使用
concat
函数执行此操作,如下所示:

select concat('No result found for student ID: ', stuID, ' at year: ', stuYear)
顺便说一句,使用concat函数的正常赋值也应该在触发器中工作:

SET msg = concat('No result found for student ID: ', stuID, ' at year: ', stuYear);

PS:在下一个语句中,还有括号:
(credits*100)

在这种情况下,它会意外地起作用,因为它是一个表达式。不过,它们没有任何功能价值,最好将其删除。

@Jay2019。如果这回答了你的问题,你应该考虑接受答案。