我需要插入大约 150 行简单行(一个 ID 和一个静态状态“丢弃”)。我有一串 ID:
'123', '234r', '345', '456xyz'...
使用这串 ID 插入行的最简单方法是什么?
似乎有某种方法可以用逗号分隔字符串并...创建一个临时表来...?我不知道 - 这似乎是 MySQL 经常设法以一些很酷、权宜之计的方式实现的事情。
最佳答案
如何使用几行 PHP 创建 INSERT 语句的示例:
<?php
// copy your string of ids into this variable
$input = "'123', '234r', '345', '456xyz'";
// modify next line to get your desired filename
$filename = 'insert.sql'
// modify next line to your table name
$insert_statement = "INSERT INTO your_table_name (id, status) VALUES \n" .
'(' . implode(", 'discard')\n(", explode(', ', $input)) . ", 'discard');\n";
file_put_contents($filename, $insert_statement);
?>
注意
这是针对这个特殊用例的。如果 id 字符串中包含一些特殊字符,例如单引号,那么这种简单的方法将失败。
关于MySQL:使用 ID 字符串插入行的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25039375/