我有这段代码。
$db_update_syntax = array(
"INSERT INTO posts (SELECT NULL,'TEMP','".$data->cid."',title,img,redirect,redirect_full,site_name,description,longurl,shorturl,publishdate,publishtime,platform,post_type,youth,volunteers,biomed,img_idea,client,category,sme,approved,featured,announce,dynamic_content,trash,archive,NULL FROM posts WHERE cid='".$orig_cid."' AND trash != 1)",
"SET @subdomain = (SELECT SUBSTRING_INDEX(guid, '-', 1) FROM posts WHERE cid ='".$orig_cid."' limit 1)",
"SET @html = (SELECT LEFT(longurl, LENGTH(longurl) - LOCATE('/', REVERSE(longurl))+1) FROM posts WHERE cid ='".$orig_cid."' limit 1)",
"SET @postid = (SELECT id FROM posts WHERE cid ='".$orig_cid."' limit 1)",
"SET @newpostid = (SELECT id FROM posts WHERE cid ='".$data->cid."' limit 1)",
"UPDATE posts SET guid=CONCAT(@subdomain,'-',id) WHERE cid='".$data->cid."'",
"UPDATE posts SET longurl=CONCAT(@html,id,'.html') WHERE cid='".$data->cid."'",
"INSERT INTO platform_xrf (SELECT NULL,0,platformid FROM platform_xrf WHERE postid=@postid)",
"UPDATE platform_xrf SET postid=@newpostid WHERE postid=0"
);
我有三个表:post table,platform table和platform_xrf table。
与每个帖子关联的平台存储在platform_xrf表中。
当帖子重复时,我还需要复制platform_xrf表中的关联行,然后使用新的帖子ID更新这些新行。
我遇到的问题是我的SET @postid和SET @newpostid。
如果我拿出LIMIT 1,查询会失败,但是如果我保留它并且有两个或更多帖子被复制,它将只复制一个。
答案 0 :(得分:0)
更改上一个INSERT
,以便它使用原始查询来复制所有行,而不是设置变量。
INSERT INTO platform_xrf
SELECT NULL, 0, platformid
FROM platform_xrf AS x
JOIN posts AS p ON p.id = x.postid
WHERE p.cid = $orig_cid
UPDATE platform_xrf AS x
CROSS JOIN posts AS p
SET x.postid = p.id
WHERE x.postid = 0
AND p.cid = $data->cid
如果@newpostid
的查询返回多个ID,我不确定您预计会发生什么。您只能将其中一个存储到platform_xrf
的所有postid = 0
行中。