Mysql

錯誤程式碼:1136。列計數與第 1 行的值計數不匹配

  • April 29, 2020

我在這裡掙扎。這是我的程式碼

CREATE DEFINER=`****`@*****` PROCEDURE `setCountry`(
   IN `countryname` VARCHAR(100) CHARSET utf8mb4,
   IN `nationality` VARCHAR(100) CHARSET utf8mb4,
   IN `countryPrefix` INT(3)
)
   MODIFIES SQL DATA
INSERT INTO country(
   countryName,
   nationality,
   countryPrefix
)
SELECT
   @countryname
FROM
   (
   SELECT
       @countryname AS countryName,
       @nationality AS nationality,
       @countryprefix AS countryPrefix
) AS tmp
WHERE NOT EXISTS
   (
   SELECT
       countryName
   FROM
       country
   WHERE
       countryName = @countryname
)
LIMIT 1

我正在嘗試用這句話執行程式碼

call ccc.setCountry('Togo', 'Togolese', 228);

它顯示的錯誤是這樣的:

> > 錯誤程式碼:1136。列計數與第 1 行的值計數不匹配 > > >

您需要為插入選擇 3 列。

和 @countryname 和 countryName 是不同的變數

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `setCountry`(
   IN `countryname` VARCHAR(100) CHARSET utf8mb4,
   IN `nationality` VARCHAR(100) CHARSET utf8mb4,
   IN `countryPrefix` INT(3)
)
   MODIFIES SQL DATA
BEGIN
     SET @countryname := countryName;
     SET  @nationality := nationality;
     SET @countryprefix := countryPrefix;

   INSERT INTO country(
       countryName,
       nationality,
       countryPrefix
   )
   SELECT    
       countryname,
       nationality,
       countryPrefix
   FROM
       (
       SELECT
           @countryname AS countryName,
           @nationality AS nationality,
           @countryprefix AS countryPrefix
   ) AS tmp
   WHERE NOT EXISTS
       (
       SELECT
           countryName
       FROM
           country
       WHERE
           countryName = @countryname
   )
   LIMIT 1;
END$$

DELIMITER ;

引用自:https://dba.stackexchange.com/questions/266094