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

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"

Related

Insert multiple rows for each of a result set?

my google-fu is failing me.
I'm trying to do the following in a more automatic way:
1) select a set of fields from 1 table
select ACCT_ID from MASTER_ACCT where CUST_NBR like '%ABC';
2) use the results of that in a multiple row insert
// for each ACCT_ID in (1)
insert into TOGGLES (FIELD1, FIELD2, FIELD3)
values('abc', '123', ACCT_ID[i]);
Is there a way to execute the 2nd statement for ACCT_ID[i] in each of the ACCT_ID results from the 1st statement?
You would use an INSERT INTO...SELECT statement:
INSERT INTO toggles (field1, field2, field3)
SELECT 'abc', '123', acct_id
FROM master_acct
WHERE cust_nbr LIKE '%ABC';
You might use the below syntax
INSERT INTO target_table[()] SELECT ... FROM ...;
find this link for more details.

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

Is this possible with sql?

Is it possible to do something like this:
INSERT INTO table(col1, col2) VALUES(something_from_another_table, value);
With something_from_another_table being a SQL command? Like, is there something I can do that's equivelant to:
INSERT INTO table(col1, col2) VALUES((SELECT value FROM table2 WHERE id = 3), value);
Yes
INSERT INTO table(col1, col2)
SELECT value1, 'value2' FROM table2 WHERE id = 3
Where value1 is the value from the 'other table' and value2 is a constant that you've included in that select statement.
Try this:
INSERT INTO table(col1, col2)
SELECT table2.value1, value2 FROM table2 WHERE table2.id = 3;
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
take a look especially in the examples.
I would recommend reading full syntax of SELECT, UPDATE, DELETE and INSERT SQL commands to begin with. Then expand to subqueries and DDL.
Go slowly and work out examples.
You definately can. It should work similar as the example below;
INSERT INTO Store_Information (store_name, Sales, Date)
(SELECT store_name, Sales, Date FROM Sales_Information WHERE Year(Date) = 2010)
when you specify the keyword "Values" on the insert statement you are trying to insert just a value. the current way to do that is assigning the value of the "something_from_another_table" into a variable and then, make your insert
DECLARE #ANYVALUE AS VARCHAR(40)
SELECT #ANYVALUE = ANYFIELD FROM table2 WHERE id = 3
INSERT INTO table1 (FIELD1, FIELD2) VALUES(#ANYVALUE, VALUE2)
On this way always will insert one record. the other correct way will insert n record as the where statement can filter.
INSERT INTO Store_Information (store_name, Sales, Date)
(SELECT store_name, Sales, Date FROM Sales_Information WHERE Year(Date) = 2010)

select from one table and insert into another

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

Insert a part of a table into another table

I have table A and table B, same schemas.
I want to insert certain rows from table A into table B. For example, insert into table B all rows from table A with column 'abc' > 10.
Couldn't figure out how to do it
Something like this
INSERT INTO B (supplier_id, supplier_name)
SELECT supplier_id, supplier_name FROM A
WHERE abc > 10;
Make sense?
You can use the following notation:
BEGIN TRAN
INSERT INTO ExistingTable (Col1, Col2...)
SELECT Something1, Something2... FROM Table1 WHERE ...
--ROLLBACK/COMMIT
At first blush, I'd say something like:
Insert Into B
(Select * from A
Where abc > 10)