MySQL如何复制表中的一条记录并插入
展开全部
1、创建测试表,create table test_copy(id int);
2、插入测试数据,
insert into test_copy values(1);
insert into test_copy values(2);
insert into test_copy values(3);
insert into test_copy values(4);
insert into test_copy values(5);
insert into test_copy values(6);
3、查看现有表数据,select * from test_copy,
4、从原表中复制一条记录,并插入数据表,
insert into test_copy select * from test_copy where id = 1;
5、再次查询数据表,select * from test_copy,发现多了一条记录;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先把需求说一下吧。从 MSSQL 中导出一个文章表,需要插入到 PHPCMS 中的内容表 phpcms_content 去,需要做到文章可以发布到不同的栏目中去。也就是说,需要复制一条记录,并修改其 catid,再插入到表尾的位置上。
MySQL复制一条数据并插入的语句:
INSERT INTO phpcms_content (SELECT ".$r[$i]['aid']." + 520, ".$r[$i]['cateid'].", news_catid, catid, typeid, areaid, title, style, thumb, keywords, keywords, posids, url, listorder, status, userid, username, inputtime, updatetime, searchid, islink, prefix FROM phpcms_content WHERE contentid = '".$r[$i-1]['aid']."')
大致为:insert into a SELECT id+1, ...(其它字段) FROM a ;
下面PHP具体程序:
$query = "SELECT * FROM articleincategory ORDER BY ArticleID ";
$result = $connector -> query($query);$i = 0;while($myrow = $connector -> fetch_array($result)){$r[$i]['aid'] = $myrow["ArticleID"];
$r[$i]['cateid'] = $myrow["CategoryID"];$i++;}
for($i = 0; $i < count($r); $i++){if($i > 0){if( $r[$i]['aid'] == $r[$i-1]['aid'] ){echo '第 '. $i. ' 条数据 '. $r[$i]['aid'] .' 与前一条数据 '. $r[$i-1]['aid'] .' 重复'.'/>';
$sql = " INSERT INTO phpcms_content (SELECT ".$r[$i]['aid']." + 520, ".$r[$i]['cateid'].", news_catid, catid, typeid, areaid, title, style, thumb, keywords, keywords, posids, url, listorder, status, userid, username, inputtime, updatetime, searchid, islink, prefix FROM phpcms_content WHERE contentid = '".$r[$i-1]['aid']."') ";
//$sql = " INSERT INTO phpcms_c_news (SELECT ".$r[$i]['aid']." + 520, template, titleintact, content, groupids_view, readpoint, author, copyfrom, paginationtype, maxcharperpage, sub_title FROM phpcms_c_news WHERE contentid = '".$r[$i-1]['aid']."') ";
echo $sql.' />';
//$result = $connector -> query($sql);
//INSERT INTO test (SELECT id + 10, name, class, score FROM test WHERE id = '1');}
else if( $r[$i]['aid'] != $r[$i-1]['aid'] ){$sql = " UPDATE phpcms_content SET origin_cateid = '".$r[$i]['cateid']."' WHERE contentid = '".$r[$i]['aid']."' ";
echo $sql.' />';
//$result = $connector -> query($sql);}}}如果不需要插入
MySQL复制一条数据并插入的语句:
INSERT INTO phpcms_content (SELECT ".$r[$i]['aid']." + 520, ".$r[$i]['cateid'].", news_catid, catid, typeid, areaid, title, style, thumb, keywords, keywords, posids, url, listorder, status, userid, username, inputtime, updatetime, searchid, islink, prefix FROM phpcms_content WHERE contentid = '".$r[$i-1]['aid']."')
大致为:insert into a SELECT id+1, ...(其它字段) FROM a ;
下面PHP具体程序:
$query = "SELECT * FROM articleincategory ORDER BY ArticleID ";
$result = $connector -> query($query);$i = 0;while($myrow = $connector -> fetch_array($result)){$r[$i]['aid'] = $myrow["ArticleID"];
$r[$i]['cateid'] = $myrow["CategoryID"];$i++;}
for($i = 0; $i < count($r); $i++){if($i > 0){if( $r[$i]['aid'] == $r[$i-1]['aid'] ){echo '第 '. $i. ' 条数据 '. $r[$i]['aid'] .' 与前一条数据 '. $r[$i-1]['aid'] .' 重复'.'/>';
$sql = " INSERT INTO phpcms_content (SELECT ".$r[$i]['aid']." + 520, ".$r[$i]['cateid'].", news_catid, catid, typeid, areaid, title, style, thumb, keywords, keywords, posids, url, listorder, status, userid, username, inputtime, updatetime, searchid, islink, prefix FROM phpcms_content WHERE contentid = '".$r[$i-1]['aid']."') ";
//$sql = " INSERT INTO phpcms_c_news (SELECT ".$r[$i]['aid']." + 520, template, titleintact, content, groupids_view, readpoint, author, copyfrom, paginationtype, maxcharperpage, sub_title FROM phpcms_c_news WHERE contentid = '".$r[$i-1]['aid']."') ";
echo $sql.' />';
//$result = $connector -> query($sql);
//INSERT INTO test (SELECT id + 10, name, class, score FROM test WHERE id = '1');}
else if( $r[$i]['aid'] != $r[$i-1]['aid'] ){$sql = " UPDATE phpcms_content SET origin_cateid = '".$r[$i]['cateid']."' WHERE contentid = '".$r[$i]['aid']."' ";
echo $sql.' />';
//$result = $connector -> query($sql);}}}如果不需要插入
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询