Error Code: 1241 Operand should contain 1 column(s)

7.3k Views Asked by At

create a function in mysql,when a function is call i got "Error Code: 1241 Operand should contain 1 column(s)" how to solve the error

drop function if exists age;

DELIMITER //

CREATE FUNCTION age (Dob date,Username varchar(30))

RETURNS INT

DETERMINISTIC

BEGIN

DECLARE age int;

set age=(SELECT *, YEAR(CURDATE()) - YEAR(Dob)   FROM per_det where username=Username);

return age;

END;//

delimiter ;

select * from per_det;

i got "Error Code: 1241

Operand should contain 1 column(s)" how to solve the error

1

There are 1 best solutions below

0
On

You're selecting multiple columns in the SET subquery, while age expects to be a single INT value. You don't want the * here:

set age=(SELECT YEAR(CURDATE()) - YEAR(Dob)   FROM per_det where username=Username);