select from one table and insert into another - sql

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.

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

You should use the following SQL query:
INSERT INTO Target(A, B, C)
SELECT A, B, C
FROM Source

According to the the MySQL reference for INSERT SELECT:
INSERT INTO table_name SELECT FROM other_table [ WHERE ... something ... ]

insert into table2(columnname)select columnname from table1

use this method
INSERT INTO destination (column names ) (select columnaes from example 3 );
Column should be same type here .

INSERT INTO Table_1 (column_1, column_2, column_3)
SELECT column_1, column_2, column_3
FROM Table_2
You can also set some conditions for inserting by as follows:
INSERT INTO Table_1 (column_1, column_2, column_3)
SELECT column_1, column_2, column_3
FROM Table_2
WHERE (Condition)

// if dataset is unique then only we can append data to master table otherwise no data will be appended
// To append unique data you can use this logic
INSERT INTO ut_axis_karvy_master
(sr_no, funding_ac, funding_nm, micr_no,abc)
SELECT
t1.sr_no,
t1.funding_ac,
t1.funding_nm,
t1.micr_no,
t2.id
from ut_axis_karvy
left join
ut_axis_karvy_master as t2
on t1.micr_no = t2.micr_no
having t2.id is null;

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

Related

Insert new records with several ids from another table

I have a table, which has 9 records with id = 1 - 9 (for example, there can be more than 20 ids).
I have one varchar value = 'premium'.
I need to insert these values to another table, after this action I should have 9 records with id from the first table and 'premium' varchar in the second table:
1, 'premium';
2, 'premium';
etc.
How to write the function for SQL?
Are you looking for insert . . . select or create table as?
insert into table2 (id, value)
select id, 'premium'
from table1;
or:
create table table2 as
select id, 'premium' as value
from table1;
Do you want this?
demo:db<>fiddle
INSERT INTO second_table (id, text_value)
SELECT id, 'premium'
FROM first_table;

insert into value in select statement mutliple values

I try to insert into table with 1 column is (select from table).
I should copy all the id to table1 with 1 column is (select from table )
This not working:
insert into table1 (id,resoucrce,rate) values ((select id from table2),0,0)
I want to do something like that insert all the id from table to another table with default values.
Use insert . . . select:
insert into table1 (id, resource, rate)
select id, 0, 0
from table2;
For copy the content of one table to another table within the same database use this :-
INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
or your query like that :-
Insert into table1 (id, resource, rate) select id, 0, 0 from table2;
You should focus "values" keyword;
values ((select id from table2),0,0)
When you use values(, , ,) you have to specify value columns. But you're trying to pass both resultset and single values together. That's why you get an error. You should only pass single values...
If its clear for you; you can easily find the correct sql syntax.

Combining INSERT SELECT with constants

I'm trying to insert records into a table where one of the columns comes from another table.
The other two columns are the same for each record.
All three columns are keys.
I'm trying this embedded INSERT SELECT which I see is not allowed?
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C)
VALUES (1,(SELECT COLUMN_NAME FROM TABLE) ,2)
Your syntax is off - this is the correct syntax for this:
Insert Into Table
(Column_A, Column_B, Column_C)
Select 1, Column_Name, 2
From OtherTable
I would post a comment but I don't have enough 'status' yet.
The insert below works in PostgreSQL. What database are you using?
insert into people_experiences (qty, created_at, updated_at, person_id, experience_id) values (1, now(), now(), (select id from people where first_name='Joseph'), (select id from experiences where experience='MS'));
INSERT 0 1
We can even use this
Insert Into Table
(Select "value-1" as column, "value-2" as column, ot.* from otherTable ot)
for two column i want constant values and rest should be use as it is.
Note: Insert select will not allow virtual column.

Insert values from another Table in sql server 2008

I have to copy all column data to another table.I have created a new blank table.How to insert the values in it.I am avoiding writing the column name manually because it contain 35 column name in it.Sequence & name of column are same in both the table..?
If the tables have the same columns and types, just do;
INSERT INTO table2 SELECT * FROM table1;
Demo here.
use following stcript:
INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2"
for more information see:
http://www.1keydata.com/sql/sqlinsert.html
Create table2 with columns and datatype for each column. If the columns match up exactly on both tables, then insert into table2 from table1
Create table table2(
column1 datatype,
column2 datatype,
column3 datatype,
column35 datatype
}
INSERT INTO table2
SELECT * from table1
create table2
insert into table2
select * from table1
Please find my verion . i had same column name in both tables
INSERT INTO first_table
(column_1,
column_2,
column_3,
column_etc)
SELECT tab2.column_1 AS column_1,
10 AS column_2,
Getdate() AS column_3,
'some_text' AS column_etc
FROM second_table tab2 (nolock)
insert into dbo.FolderStatus
(
[FolderStatusId],
[code],
[title],
[last_modified]
)
select
[code],
[code],
[title],
[last_modified]
from dbo.f_file_stat

How to insert values into an MYSQL-DB via subselect?

In oracle-db it is possible to insert values from table A to table B like
Insert into table_a values
Select * from table_b where ID = 10
;
If the structures are the same.
How can I do this in MYSQL? My Editor gave me an sytanx-error.
Thx 4 your answers!
Greetz
Mysql does not use the values keyword:
Insert into table_a (field1, field2)
Select field1, field2 from table_b where table_b.ID = 10;
In oracle and mysql:
insert into test
select 1 from dual
Not: "insert into table values"