[英]copy mysql data to same the same table
我需要能夠將沒有userid的所有行復制到具有等於1的userid的同一表中。這是我的代碼:(運行時不起作用),它確實顯示了項目字段,並且使用echo的窗口小部件ID字段。 但它不會插入數據。 有什么建議么
$sql=("SELECT * from options WHERE userid='' ");
$resultat= mysql_query($sql);
while($row = mysql_fetch_array($resultat))
{
$userid="1";
echo $items.$widgetid."<br>";
$widgetid = $row['widgetid'];
$items = $row['items'];
$stats = $row['stats'];
$upd=("INSERT INTO options (userid, widgetid, items,stats)
VALUES ('$userid', '$widgetid','$items', '$stats')");
mysql_query($upd);
}
INSERT INTO options
SELECT '1' AS userid, widgetid, items, stats
FROM options
WHERE userid = ''
假設這是表中聲明的列的順序。
我認為您只需要更新userid =''的行
update options
set userid=1
where userid='';
$upd=("INSERT INTO options (userid, widgetid, items,stats)
select 1, widgetid, stats from options
where userid='' ");
create table newTable like oldTable;
insert into newTable
select * from oldTable
where userid = '';
但是也許您真的只需要像Naveen所說的那樣進行更新?
在PHP中使用以下代碼段
$upd= (
"INSERT INTO
options (userid, widgetid, items,stats)
VALUES (1, '". $widgetid ."' , '". $items ."' , '". $stats ."')
"
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.