【问题标题】:MySQL While Loop to fillup Table with incremental values where Auto-Increment is no optionMySQL While 循环用增量值填充表,其中自动增量不是选项
【发布时间】:2018-09-09 14:37:29
【问题描述】:

我现在尝试用增量值填充 MySQL 表。稍后可能会发生变化,为什么 AUTO-INCREMENT 在这里没有解决方案。 我在 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 */;

【问题讨论】:

  • 缺失;在 insert 之后,缺少 end if;可能未设置分隔符。
  • 改变了你的建议 P.Salmon 在同一行仍然有相同的错误消息。
  • 使用调用 p();在末尾。这基本上是缺失的一点
  • 第一个问题是DELIMITER ;

标签: mysql if-statement stored-procedures while-loop mariadb


【解决方案1】:

有更正

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)

【讨论】:

  • 错误消失了,但它没有向 DB-Table 发布任何内容。这是否与 settingsOperationMixerId 为 AUTO-INCREMENT 或我在 phpMyAdmin SQL 命令行中使用此语句有关
  • 在问题中将其更改为 Value ( NULL,... )。仍然没有错误消息,但表中也没有条目
  • auto_increment 列是主键吗?
  • 是的,它是一个主键,只是用于表 ID 设置OperationMixerId
  • 如果您希望 settingsOperationMixerId 自动增加,则将其从插入列表中删除并从值列表中删除。如果你想覆盖 auto_increment 确保 a 是唯一的。
【解决方案2】:

mariadb 中的一个单行,使用Sequence storage engine

insert into t(a,b,c) select seq%6+1,seq+1,1 from seq_0_to_63;

【讨论】:

  • 其实seq_1_to_63,因为他是从1开始的。
猜你喜欢
  • 2017-10-27
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 2015-02-12
  • 2023-03-14
  • 2011-11-07
相关资源
最近更新 更多