从一个表中选择并插入另一个表

时间:2021-07-01 00:28:29

I've got two tables.

我有两张桌子。

Table_A (nid, vid, type, title, uid)

Table_A(nid,vid,类型,标题,uid)

Table_B (id, questiontext)

Table_B(id,questiontext)

I need to insert records from Table_B into Table_A. I tried this:

我需要将Table_B中的记录插入Table_A。我试过这个:

INSERT INTO Table_A (nid, vid, type, title, uid)
VALUES ('', '', multichoice', (SELECT questiontext from Table_B), '1')

but it's throwing an error.

但这是一个错误。

What should be the correct statement?

什么应该是正确的声明?

UPD: I should add that nid is autoincrement and the value of vid should be same as nid.

UPD:我应该补充一点,nid是自动增量,vid的值应该与nid相同。

6 个解决方案

#1


47  

Have you tried

你有没有尝试过

INSERT INTO Table_A (nid, vid, type, title, uid) 
SELECT  '', 
        '', 
        'multichoice', 
        questiontext ,
        '1'
from    Table_B

Have a look at INSERT ... SELECT Syntax

看看INSERT ... SELECT语法

#2


8  

You should use the following SQL query:

您应该使用以下SQL查询:

INSERT INTO Target(A, B, C)
  SELECT A, B, C
    FROM Source

#3


3  

According to the the MySQL reference for INSERT SELECT:

根据INSERT SELECT的MySQL参考:

INSERT INTO table_name SELECT FROM other_table [ WHERE ... something ... ]

#4


0  

use this method

使用这种方法

INSERT INTO destination (column names ) (select columnaes from example 3 );

INSERT INTO目的地(列名)(从示例3中选择columnaes);

Column should be same type here .

列在这里应该是相同的类型。

#5


0  

insert into table2(columnname)select columnname from table1

#6


-1  

I think the correct answer to this might be select into, from what I see from the other answers is that you guys insert before getting the value from table B as you should first get the value that is : SELECT from table B then insert into table A. you should be searching on the lines of select into

我认为对此的正确答案可能是选择,从我从其他答案中看到的是你们在从表B获取值之前插入,因为你应该首先得到的值是:从表B中选择然后插入到表中答:你应该在选择的行上搜索

#1


47  

Have you tried

你有没有尝试过

INSERT INTO Table_A (nid, vid, type, title, uid) 
SELECT  '', 
        '', 
        'multichoice', 
        questiontext ,
        '1'
from    Table_B

Have a look at INSERT ... SELECT Syntax

看看INSERT ... SELECT语法

#2


8  

You should use the following SQL query:

您应该使用以下SQL查询:

INSERT INTO Target(A, B, C)
  SELECT A, B, C
    FROM Source

#3


3  

According to the the MySQL reference for INSERT SELECT:

根据INSERT SELECT的MySQL参考:

INSERT INTO table_name SELECT FROM other_table [ WHERE ... something ... ]

#4


0  

use this method

使用这种方法

INSERT INTO destination (column names ) (select columnaes from example 3 );

INSERT INTO目的地(列名)(从示例3中选择columnaes);

Column should be same type here .

列在这里应该是相同的类型。

#5


0  

insert into table2(columnname)select columnname from table1

#6


-1  

I think the correct answer to this might be select into, from what I see from the other answers is that you guys insert before getting the value from table B as you should first get the value that is : SELECT from table B then insert into table A. you should be searching on the lines of select into

我认为对此的正确答案可能是选择,从我从其他答案中看到的是你们在从表B获取值之前插入,因为你应该首先得到的值是:从表B中选择然后插入到表中答:你应该在选择的行上搜索