gpt4 book ai didi

MySQL While 循环用增量值填充表,其中没有自动增量选项

转载 作者:行者123 更新时间:2023-11-29 06:45:42 26 4
gpt4 key购买 nike

我尝试用目前的增量值填充 MySQL 表。稍后可能会发生变化,为什么自动增量在这里不是解决方案。 我正在 phpMyAdmin 的 SWL 控制台中工作

只有settingsOperationMixerId启用了自动增量

我尝试做一个循环:

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)

关于MySQL While 循环用增量值填充表,其中没有自动增量选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49576361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com