#1222 - The used SELECT statements have a different number of columns

后端 未结 5 736
Happy的楠姐
Happy的楠姐 2021-02-13 19:55

Why am i getting a #1222 - The used SELECT statements have a different number of columns ? i am trying to load wall posts from this users friends and his self.

S         


        
5条回答
  •  借酒劲吻你
    2021-02-13 20:33

    Beside from the answer given by @omg-ponies; I just want to add that this error also occur in variable assignment. In my case I used an insert; associated with that insert was a trigger. I mistakenly assign different number of fields to different number of variables. Below is my case details.

    INSERT INTO tab1 (event, eventTypeID, fromDate, toDate, remarks)
        -> SELECT event, eventTypeID, 
        -> fromDate, toDate, remarks FROM rrp group by trainingCode;
    ERROR 1222 (21000): The used SELECT statements have a different number of columns
    

    So you see I got this error by issuing an insert statement instead of union statement. My case difference were

    1. I issued a bulk insert sql

      i.e. insert into tab1 (field, ...) as select field, ... from tab2

    2. tab2 had an on insert trigger; this trigger basically decline duplicates

    It turns out that I had an error in the trigger. I fetch record based on new input data and assigned them in incorrect number of variables.

    DELIMITER @@
    DROP TRIGGER trgInsertTrigger @@
    CREATE TRIGGER trgInsertTrigger
    BEFORE INSERT ON training
    FOR EACH ROW
    BEGIN
    SET @recs = 0;
    SET @trgID = 0;
    SET @trgDescID = 0;
    SET @trgDesc = '';
    SET @district = '';
    SET @msg = '';
    
    SELECT COUNT(*), t.trainingID, td.trgDescID, td.trgDescName, t.trgDistrictID
        INTO @recs, @trgID, @trgDescID, @proj, @trgDesc, @district
        from training as t
        left join trainingDistrict as tdist on t.trainingID = tdist.trainingID
        left join trgDesc as td on t.trgDescID = td.trgDescID
        WHERE
        t.trgDescID = NEW.trgDescID
        AND t.venue = NEW.venue
        AND t.fromDate = NEW.fromDate 
        AND t.toDate = NEW.toDate 
        AND t.gender = NEW.gender
        AND t.totalParticipants = NEW.totalParticipants
        AND t.districtIDs = NEW.districtIDs;
    
    IF @recs > 0 THEN
        SET @msg = CONCAT('Error: Duplicate Training: previous ID ', CAST(@trgID AS CHAR CHARACTER SET utf8) COLLATE utf8_bin);
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @msg;
    END IF;
    END @@ 
    
    DELIMITER ; 
    

    As you can see i am fetching 5 fields but assigning them in 6 var. (My fault totally I forgot to delete the variable after editing.

提交回复
热议问题