简体   繁体   中英

Error Code: 1241. Operand should contain 1 column(s) don't know how to fix this

DELIMITER //
CREATE PROCEDURE positioner(objectivedate date)
BEGIN
    DECLARE var1 INT(3) DEFAULT 0;
    DECLARE var2 INT(3) DEFAULT 0;
    DECLARE var3 INT(3) DEFAULT 0;
    
    SET var1 = (SELECT COUNT(CID) FROM erd.caddy WHERE (WorkDate = objectivedate, WorkTime = '1'));
    SET var2 = (SELECT COUNT(CID) FROM erd.caddy WHERE (WorkDate = 'objectivedate', WorkTime = '2'));
    SET var3 = (SELECT COUNT(CID) FROM erd.caddy WHERE (WorkDate = 'objectivedate', WorkTime = '3'));
    SELECT var1 AS '1부',var2 AS '2부', var3 AS '3부';
END; //
DELIMITER ;

Hello I am trying to make a delimiter that counts number of CID by WorkTime (which means work session consists of 1,2,3) by putting certain WorkDate (which I set as objectivedate).

When I put CALL("2020-05-11")

The error says that:

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

I am certain that it's about SET var1... row, but I don't know how to fix this. I am desperate!!!

You have a syntax error in your where clause of all 3 queries

write like below:

var1= SELECT COUNT(CID) FROM erd.caddy WHERE WorkDate=objectiveDate and WorkTime='1'

You need to use boolean expressions for your conditions.

For example you have:

WHERE (WorkDate = objectivedate, WorkTime = '1')

But it should be:

WHERE (WorkDate = objectivedate AND WorkTime = '1')

Also I'm not sure you intended this:

WorkDate = 'objectivedate'

Putting 'objectivedate' in the single-quotes makes it a literal string 'objectivedate', not the variable objectivedate which is the date input parameter of the procedure.

Finally, I can suggest a simplification to your procedure:

CREATE PROCEDURE positioner(objectivedate date)
BEGIN
    SELECT 
        COUNT(CASE WorkTime WHEN '1' THEN true END) AS '1부',
        COUNT(CASE WorkTime WHEN '2' THEN true END) AS '2부',
        COUNT(CASE WorkTime WHEN '3' THEN true END) AS '3부'
    FROM erd.caddy
    WHERE WorkDate = objectivedate;
END

This gets all three counts in one query, and you can skip declaring the local variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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