我现在尝试用增量值填充MySQL表。稍后可能会发生变化,为什么在这里自动解决方案无法解决。我正在phpMyAdmin的SWL控制台中工作
仅settingsOperationMixerId启用了AUTO-INCREMENT
我试图做一个循环:
DELIMITER ;
CREATE PROCEDURE fill()
BEGIN
DECLARE v1 INT DEFAULT 1;
DECLARE b INT DEFAULT 1; #counting upwards
DECLARE c INT DEFAULT 1; #only 1 mixer
DECLARE d INT DEFAULT 1; #product from 1-6
WHILE v1 < 64 DO
INSERT INTO `settingsoperationmixer`(`settingsOperationMixerId`,
`settingsOperationId`, `mixerId`, `productId`) VALUES (NULL,b,c,d);
IF d < 6 THEN SET d = d + 1;
ELSE SET d = 1;
END IF;
SET b = b + 1;
SET v1 = v1 + 1;
END WHILE;
END;
DELIMITER ;
这应该循环通过63次,同时settingsOperationId将是1-63增量,并且productId应该从1-6变大,然后从1重新开始
并得到以下错误信息:
Error
SQL query:
CREATE PROCEDURE fill()
BEGIN
DECLARE v1 INT DEFAULT 1
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use near '' at line 3
我的表格的SQL-CODE可帮助解决问题:
-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 30, 2018 at 05:13 PM
-- Server version: 10.1.30-MariaDB
-- PHP Version: 7.2.2
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `scm`
--
-- --------------------------------------------------------
--
-- Table structure for table `settingsoperationmixer`
--
CREATE TABLE `settingsoperationmixer` (
`settingsOperationMixerId` int(11) NOT NULL,
`settingsOperationId` int(11) NOT NULL,
`mixerId` int(11) NOT NULL,
`productId` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `settingsoperationmixer`
--
ALTER TABLE `settingsoperationmixer`
ADD PRIMARY KEY (`settingsOperationMixerId`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `settingsoperationmixer`
--
ALTER TABLE `settingsoperationmixer`
MODIFY `settingsOperationMixerId` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
最佳答案
有更正
drop procedure if exists p;
delimiter $$
CREATE PROCEDURE p()
BEGIN DECLARE v1 INT DEFAULT 1;
DECLARE a INT DEFAULT 1; #autoIncrement
DECLARE b INT DEFAULT 1; #counting upwards
DECLARE c INT DEFAULT 1; #only 1 mixer
DECLARE d INT DEFAULT 1; #product from 1-6
WHILE v1 < 64 DO
INSERT INTO `settingsoperationmixer`(`settingsOperationMixerId`,`settingsOperationId`, `mixerId`, `productId`) VALUES (a,b,c,d);
IF d < 6 THEN
SET d = d + 1;
ELSE
SET d = 1;
end if;
SET b = b + 1;
SET v1 = v1 + 1;
END WHILE;
END $$
delimiter ;
如果要允许应用auto_increment,则不要将其包含在插入列表中
drop procedure if exists p;
delimiter $$
CREATE PROCEDURE p()
BEGIN DECLARE v1 INT DEFAULT 1;
DECLARE a INT DEFAULT 1; #autoIncrement
DECLARE b INT DEFAULT 1; #counting upwards
DECLARE c INT DEFAULT 1; #only 1 mixer
DECLARE d INT DEFAULT 1; #product from 1-6
WHILE v1 < 10 DO
INSERT INTO `settingsoperationmixer`(`settingsOperationId`, `mixerId`, `productId`) VALUES (b,c,d);
IF d < 6 THEN
SET d = d + 1;
ELSE
SET d = 1;
end if;
SET b = b + 1;
SET v1 = v1 + 1;
END WHILE;
END $$
delimiter ;
drop table if exists `settingsoperationmixer`;
create table `settingsoperationmixer`
(`settingsOperationMixerId` int auto_increment primary key ,`settingsOperationId` int, `mixerId` int, `productId` int)
;
call p();
MariaDB [sandbox]> select * from `settingsoperationmixer`;
+--------------------------+---------------------+---------+-----------+
| settingsOperationMixerId | settingsOperationId | mixerId | productId |
+--------------------------+---------------------+---------+-----------+
| 1 | 1 | 1 | 1 |
| 2 | 2 | 1 | 2 |
| 3 | 3 | 1 | 3 |
| 4 | 4 | 1 | 4 |
| 5 | 5 | 1 | 5 |
| 6 | 6 | 1 | 6 |
| 7 | 7 | 1 | 1 |
| 8 | 8 | 1 | 2 |
| 9 | 9 | 1 | 3 |
+--------------------------+---------------------+---------+-----------+
9 rows in set (0.00 sec)