簡體   English   中英

通過將數字增加3插入mysql

[英]Insert into mysql by incrementing a number by 3

我有一個mysql表來存儲用戶的身高。 我的桌子看起來像這樣:

CREATE TABLE height (
  id INT AUTO_INCREMENT,
  feets_and_inches VARCHAR(10) DEFAULT NULL,
  centimeters INT NOT NULL,
    PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

我的問題是我需要像這張91,94,97,100..... and upto 241一樣在此表中插入厘米

我可以知道是否有一種方法可以通過手動插入來使用循環插入此值。

希望有人可以幫助我。 謝謝。

我可以知道是否有一種方法可以通過手動插入來使用循環插入此值。

一種方法是

$i=91;
while($i<=241)
{
    // insert $i as centimeters in a query
    $i+=3;
}

如果您使用MySQL進行操作,則可以創建一個從91到241的循環,如下所示:

DELIMITER $$  
CREATE PROCEDURE CENTIMETERINSERT()
   BEGIN
    DECLARE count INT;
    DECLARE max INT;
    SET count=91;
    SET max= 241;
    WHILE(count < max) DO
        -- Run your insert here, replace my example select statement
        SELECT count;
        SET count=(count+3);
    END WHILE;
END $$

CALL CENTIMETERINSERT() $$

這是如何在mysql中做到這一點

BEGIN
        DECLARE i INT DEFAULT 91;
        WHILE (i<=241) DO
               //write your insert statement
               SET i=i+3;
        END WHILE;
END$$

是的,您幾乎可以使用任何類型的循環來執行此操作。 我建議如下的for循環:

for ($i=91; $i <= 241; $i +=3){
   //SQL insert code here
}

也許您可以為此創建一個小的過程,並在需要時調用它:

CREATE PROCEDURE `add_centimeters`()

BEGIN

DECLARE counter int DEFAULT 91;

WHILE (counter < 242) DO
       INSERT INTO height(centimeters) VALUES (counter);
       SET counter =  counter + 3;
END WHILE;
END;
$j = 91; 

 for($i=$j; $i<=241; $i++){

    // echo $i;
    // insert the below variable into table   
    $insert_it = $i; 

    ///  Put insert sql query below example is:
    // mysql_query("insert into tbl(`height`) values('$insert_it')");
    $i=$i+2;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM