简体   繁体   English

将记录从一张表插入到另一张表

[英]insert records from one table to another

Hi I am trying to add records from one table to another, once i have added a 'user' record, the table that is being selected contains rows of available security options, and the table that is being inserted to is the child table for the user, detailing security options.您好我正在尝试将记录从一个表添加到另一个表,一旦我添加了“用户”记录,正在选择的表包含可用安全选项的行,并且正在插入的表是子表用户,详细说明安全选项。

I cam across this code in an earlier post, which i am sure works nicely, however i am trying to modify it so that the values from statement, includes two parts, one from the select query and one which is the key from the master record.#我在之前的帖子中看到了这段代码,我确信它工作得很好,但是我正在尝试修改它,以便语句中的值包括两部分,一个来自 select 查询,另一个是来自主记录的键.#

This is the original code I found from this site:这是我从这个网站找到的原始代码:

INSERT INTO def (catid, title, page, publish) 
SELECT catid, title, 'page','yes' from `abc`

And this is what I am trying to do with it:这就是我想要做的:

$sql = "INSERT INTO Link_UserSecurity (UserFk, ModuleFk) values ('".$keys["UserPk"]."', SELECT ModulePk from Global_Modules)";
CustomQuery($sql);

And this is the error I am getting:这是我得到的错误:

INSERT INTO Link_UserSecurity (UserFk, ModuleFk) values ('4', SELECT ModulePk from Global_Modules)插入 Link_UserSecurity(UserFk,ModuleFk)值('4',SELECT ModulePk 来自 Global_Modules)

See screenshot for further detail有关更多详细信息,请参阅屏幕截图

Obviously I am not concating the from statement properly, but would appreciate any help?显然我没有正确连接 from 语句,但希望有任何帮助吗?

You can insert the $keys["UserPk"] variable as if it were a constant in the SQL:您可以插入$keys["UserPk"]变量,就好像它是 SQL 中的常量一样:

$sql = "INSERT INTO Link_UserSecurity (UserFk, ModuleFk) SELECT '{$keys["UserPk"]}', ModulePk from Global_Modules";

Do note that $keys["UserPk"] must be escaped before adding it into the query.请注意,在将$keys["UserPk"]添加到查询之前必须对其进行转义 In PDO, it would look like this:在 PDO 中,它看起来像这样:

$keys["UserPk"] = $pdo->quote($keys["UserPk"]);

$sql = "INSERT INTO Link_UserSecurity (UserFk, ModuleFk) SELECT '{$keys["UserPk"]}', ModulePk from Global_Modules";

Could be a problem related to the double quotes sequence可能是与双引号序列相关的问题

"INSERT INTO Link_UserSecurity (UserFk, ModuleFk) 
values ('". $keys['UserPk']. "', SELECT  ModulePk from Global_Modules)";

but you could use also a select insert但你也可以使用选择插入

"INSERT INTO Link_UserSecurity (UserFk, ModuleFk) 
SELECT '" . $keys['UserPk']. "' ,  ModulePk from Global_Modules)";

Adding only new and unique records from one table to another.仅将新的和唯一的记录从一个表添加到另一个表。 Limiting is a good idea to prevent it from timeout.限制是防止超时的好主意。 It can be run several times until all the records copied.它可以运行多次,直到复制所有记录。

First, select the latest record ID from the table to be copied:一、select 表中要复制的最新记录ID:

SET @lastcopied = 
(SELECT 
    IF(MAX(a.exp_inotech_id)>0, MAX(a.exp_inotech_id),  0) AS lastcopied 
FROM 
    kll_export_to a
WHERE exp_tezgah = 'A2015-0056');

Then, select and add the records to the destination table:然后, select 并将记录添加到目标表:

INSERT INTO kll_export_to
(SELECT * FROM 
    kll_export_from f
GROUP BY f.exp_inotech_id
HAVING COUNT(f.exp_inotech_id) = 1 AND exp_tezgah = 'A2015-0056' AND f.exp_inotech_id > @lastcopied
ORDER BY exp_inotech_id
LIMIT 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM