-2

我需要将一些字段从一个表复制到另一个表。

第一个表称为 phpbb_karma,第二个表是 smf_log_karma

我需要以下结构:

user_id -> id_executor
poster_id -> id_target
log_time -> karma_time
action -> karma_action

我运行了以下查询:

INSERT INTO
`smf_log_karma`
(`id_executor`, `id_target`, `log_time`, `action`)
SELECT
(`user_id`, `poster_id`, `karma_time`, `karma_action`)
AS
`id_executor`, `id_target`, `log_time`, `action`
FROM
`phpbb_karma`

它回来了

#1241 - Operand should contain 1 column(s)

任何帮助将不胜感激,谢谢。

4

7 回答 7

3
INSERT INTO
        `smf_log_karma`
        (`id_executor`, `id_target`, `log_time`, `action`)

   SELECT
        `user_id`, `poster_id`, `karma_time`, `karma_action`
        FROM
        `phpbb_karma` AS T1
 WHERE NOT EXISTS    (
                SELECT 1
                FROM `smf_log_karma` AS T2
                WHERE
               (T2.id_executor = T1.user_id) );
于 2013-07-04T08:45:27.817 回答
1

您需要从两个表中取出 (`) 符号

INSERT INTO smf_log_karma 
(id_executor, id_target, log_time, action)
SELECT user_id, poster_id, karma_time, karma_action
FROM
phpbb_karma

并确保您的表和列是相同的数据类型并且完全相同,并且如果 PK,您的 id_executor 不能重复

于 2013-07-04T08:54:30.600 回答
0

您只需要以下内容即可复制所有行:

INSERT INTO smf_log_karma 
(id_executor, id_target, log_time, action)
SELECT user_id, poster_id, karma_time, karma_action
FROM
phpbb_karma
于 2013-07-04T08:42:02.700 回答
0

尽管在选择中不需要列命名,但您需要它,但很有帮助

enter code here
 INSERT INTO smf_log_karma
     (id_executor,
      id_target,
      log_time,
      action)
 SELECT
      user_id as id_executor,
      poster_id as id_target,
      karma_time as log_time,
      karma_action as action
 FROM phpbb_karma

你可以在这里看到它的作用

于 2013-07-04T08:45:05.663 回答
0

我是数据迁移的团队成员。

所以试试这个查询,让我知道它是否有效。

INSERT INTO `smf_log_karma`
(
     `id_executor`,
     `id_target`, 
     `log_time`, 
     `action`
)
SELECT
     `user_id`, 
     `poster_id`, 
     `karma_time`, 
     `karma_action`
FROM
`phpbb_karma` AS T1
where T1.user_id
not in (select `id_executor` from `smf_log_karma`)
于 2013-07-04T10:14:13.727 回答
0
INSERT INTO `smf_log_karma` (`id_executor`, `id_target`, `log_time`, `action`)   
SELECT `user_id` AS `id_executor`, `poster_id` AS `id_target`, `karma_time` AS `log_time`, `karma_action` AS `action`
FROM `phpbb_karma`
于 2013-07-04T08:51:09.123 回答
0

你的意思是你想复制一些字段而不是全部?如果是这样,我认为您忘记在语法中使用“值”,这就是原因。您必须使用 insert into .... (...) values select ....

于 2013-07-04T08:39:00.380 回答