简体   繁体   English

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

[英]MYSQL: ERROR 1241 (21000): Operand should contain 1 column(s)

this should be working but I get an error somehow. 这应该可以工作,但是我却以某种方式出错。 I want to get the credits column from the table and multiply it by 100. The problem is to get the number of credits for a given student id and year and get the total payment. 我想从表中获取学分列并将其乘以100。问题是获取给定学生ID和年份的学分数量并获得总付款。 Assuming each credit is $100. 假设每笔赠送金额为$ 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 ;

This is incorrect: 这是不正确的:

select ('No result found for student ID: ', stuID, ' at year: ', stuYear)

A select statement can contain multiple columns, but you shouldn't enclose them in parentheses. select语句可以包含多个列,但不要将其括在括号中。 Also, that would return multiple values, which you can't select into a single message. 同样,这将返回多个值,您不能在单个消息中选择它们。

So, I guess you wanted to concat the values into a single value. 因此,我想您想将这些值合并为一个值。 You can do that using the concat function, like so: 您可以使用concat函数执行此操作,如下所示:

select concat('No result found for student ID: ', stuID, ' at year: ', stuYear)

Btw, a normal assignment using the concat function should also work in a trigger: 顺便说一句,使用concat函数的常规分配也应在触发器中工作:

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

See MySQL concat function 参见MySQL concat函数

PS: In the next statement, you also got parentheses: (credits * 100) In this case it accidentally will work, because it's a single expression. PS:在下一条语句中,您还会得到括号:( (credits * 100)在这种情况下,由于它是单个表达式,因此偶然会起作用。 They don't have any functional value, though, and might as well be removed. 但是,它们没有任何功能价值,因此也有可能被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM