tags:

views:

64

answers:

4

I've got two tables.

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

Table_B (id, questiontext)

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

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.

+6  A: 

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

astander
nid is autoincrement. how can I add the newly created nid into vid too in this statement?
NJ
A: 

Not sure for MySQL, but in general you should use the following SQL query:

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

I'm pretty sure this will work in MySQL too.

Cheers Matthias

Mudu
A: 

According to the the MySQL reference:

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

http://dev.mysql.com/doc/refman/5.1/en/insert-select.html

Dan Beam
A: 

use this method

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

Column should be same type here .

pavun_cool