恐怕这听起来有些多余。我已经尽了我的努力,并替换了至少三个我迄今为止发现的想法,但没有任何效果。
目标是合并两个表中的唯一字段,但现在我什至无法将一个表中的相同字段获取到作为相同表创建的另一个表中。这是到目前为止的代码,以及我的评论:
$result = mysql_query("SELECT * from $db.$tbl LIMIT 50");
if($result) {
while($maindata = mysql_fetch_assoc($result)) {
$newdata = implode ("," , $maindata);
$newdata = mysql_escape_string($newdata);
$pop = "INSERT INTO $db.address_new (id,entitynum,address,city,state,zip,country,remarks)
SELECT FROM $db.address";
//This is the original code replaced by the above
//$pop = "INSERT INTO $db.address_new (id,entitynum,address,city,state,zip,country,remarks)
// VALUES ($newdata)";
mysql_query($pop);
//print_r($pop);
//Seems to give the correct output in the browser, but the table address_new remains empty. `
提前谢谢您。我非常感谢您的帮助。
最佳答案
直接从另一个表插入(注意:如果 ID 是 auto_increment,您可能希望也可能不希望以这种方式插入):
INSERT INTO db.address_new (id,entitynum,address,city,state,zip,country,remarks)
SELECT (id,entitynum,address,city,state,zip,country,remarks)
FROM db.address LIMIT 50
(不要将其放入循环中)
如果您正在寻找独特的值(value),可以通过多种方式实现。就我个人而言,我会对一个(或一组)值有一个唯一的键,然后只需执行 INSERT IGNORE
:
INSERT IGNORE INTO db.address_new
(id,entitynum,address,city,state,zip,country,remarks)
SELECT (id,entitynum,address,city,state,zip,country,remarks)
FROM db.address LIMIT 50
附注:
// use mysql_real_escape_string
mysql_escape_string($newdata);
// since you're passing this through PHP, you need to make sure to quote
// all of your values. This probably means you'll need to loop and replace this
// $newdata = implode ("," , $maindata);
// with something like:
$newdata = array();
foreach( $maindata as $column )
{
$newdata[] = "'" . mysql_real_escape_string( $column ) . "'";
}
$newdata = implode(',', $newdata);
// you're missing the columns in your select clause.
$pop = "INSERT INTO $db.address_new ".
"(id,entitynum,address,city,state,zip,country,remarks) ".
// You need to select *something*
"SELECT FROM $db.address";
关于php - 将数组从一个表插入到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6824478/