[英]Extract Rows with Values >100 in a Variable Showing All Columns on MySQL Workbench
[英]MySQL Workbench - I want to loop through 100 columns which are named `1`-`100`
我正在使用 MySQL 工作台。 我正在嘗試使用循環生成的隨機虛擬數據構建一個表。 我想遍歷 100 個名為 1-100 的列。 如何做到這一點?
這是我到目前為止所擁有的。 我收到錯誤代碼:1054 “字段列表”中的未知列“計數”。
DELIMITER $$
DROP PROCEDURE IF EXISTS insertUsingLoop3$$
CREATE PROCEDURE insertUsingLoop3()
BEGIN
DECLARE count INT DEFAULT 0;
DECLARE randValue INT DEFAULT 33;
WHILE count < 101 DO
SET randValue = FLOOR( RAND() * (127-33) + 33);
INSERT INTO test1(count)
VALUES(CHAR(randValue));
SET count = count + 1;
END WHILE;
END$$
DELIMITER ;
我希望使用變量 - 'count' 作為列名,因為我使用數字 1 - 100 命名了 100 列。
您可以將查詢創建為字符串,然后執行它。 在您的情況下,查詢可能如下所示:
WHILE count < 101 DO
SET randValue = FLOOR( RAND() * (127-33) + 33);
SET @q = CONCAT('INSERT INTO test1(c', count,') VALUES(CHAR(',randValue, '))');
PREPARE stmt FROM @q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET count = count + 1;
END WHILE;
將 PHP 與 WAMP 結合使用,我能夠找到解決我試圖做的事情的方法。 這是我想出的。
function insColmns() {
// loop through columns named '1', '2', ... '98', '99', '100'
$counter1 = 1;
$colmns = '';
while($counter1 <= 100) {
$strCounter1 = (string) $counter1;
if($counter1 < 100) {
$colmns .= 'c' . $strCounter1 . ', ';
} else {
$colmns .= 'c' . $strCounter1;
}
$counter1++;
}
return $colmns;
}
function insData() {
// loop to insert random character into each of the columns
$counter2 = 1;
$data = '';
while($counter2 <= 100) {
$randValue = rand(384,591);
$unicodeChar1 = (string) mb_chr($randValue);
if($counter2 < 100) {
$data .= '"' . $unicodeChar1 . '", ';
} else {
$data .= '"' . $unicodeChar1 . '"';
}
$counter2++;
}
return $data;
}
$sql = 'INSERT INTO testing.sample2 (' . insColmns() .
')
VALUES (' . insData() . ')';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.