I'd like to join 2 tables together. TableB has 15 distinct values that I want to go into the TableA (the one I'm inserting into). However, I also want to insert individual values for TableA. For example, I want to insert the 15 individual values from TableB into TableA, but I also want to be able to insert another field ('region') in TableA
--so far I have this
insert into TableA ((id)
select distinct(id) from TableB
group by id), region values('NYC')
I'm not sure how to insert the region in there...the above fails. I need to hardcore the regions in there because they are not in the other table.
You probably want something like this:
insert into TableA (id, region)
select distinct id, 'NYC'
from TableB
INSERT INTO TableA (ID, REGION)
SELECT ID, 'NYC'
FROM TableB
GROUP BY ID
That's it.
The DISTINCT is redundant.
Try something like this:
insert into TableA (id, region)
select distinct id, "NYC"
from TableB
group by id
Related
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.
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.
So for each distinct value in a column of one table I want to insert that unique value into a row of another table.
list = select distinct(id) from table0
for distinct_id in list
insert into table1 (id) values (distinct_id)
end
Any ideas as to how to go about this?
Whenever you think about doing something in a loop, step back, and think again. SQL is optimized to work with sets. You can do this using a set-based query without the need to loop:
INSERT dbo.table1(id) SELECT DISTINCT id FROM dbo.table0;
There are some edge cases where looping can make more sense, but as SQL Server matures and more functionality is added, those edge cases get narrower and narrower...
insert into table1 (id)
select distinct id from table0
The following statement works with me.
insert into table1(col1, col2) select distinct on (col1) col1 col2 from table0
The below query will also check the existing data in the Table2.
INSERT INTO Table2(Id) SELECT DISTINCT Id FROM Table1 WHERE Id NOT IN(SELECT Id FROM Table2);
Other Simple way to copy distinct data with multiple columns from one table to other
Insert into TBL2
Select * from (Select COL1, ROW_NUMBER() over(PARTITION BY COL1 Order By COL1) AS COL2 From TBL1)T
where T.COL2 = 1
I got this already working;
INSERT INTO TermsFinal
(old_classification, count, new_classification, old_term,new_term)
SELECT old_classification , Count(seed) AS count , new_classification, old_term, new_term FROM TermsTemp
GROUP BY old_classification
ORDER BY count DESC
There is one more field in the TermsFinal called SOURCE_TABLE which TermsTemp does not have.
I would like to populate that field too. I already got the $source_table value. I tried this but dis not work.
INSERT INTO TermsFinal
(SOURCE_TABLE,old_classification, count, new_classification, old_term,new_term)
'{$SOURCE_TABLE}', SELECT old_classification , Count(seed) AS count , new_classification, old_term, new_term FROM TermsTemp_TEMP
GROUP BY old_classification
ORDER BY count DESC
How do you add that value into the SOURCE_TABLE field of the TermsFinal while executing the insert into statement in one go?
and the other puzzling thing to me here, how come my first SQL insertinto works without the SQL keyword VALUES. This page http://www.w3schools.com/sql/sql_insert.asp teaches that VALUES part is needed!
You can put string (or any other type ) constant into select, for example
select 'string' as const_str , field1 from table1 will return 2 columns , the first will have "string" text for all rows. In your case you can do
INSERT INTO TermsFinal
(SOURCE_TABLE,old_classification, count, new_classification, old_term,new_term)
SELECT '{$SOURCE_TABLE}', old_classification , Count(seed) AS count , new_classification, old_term, new_term FROM TermsTemp_TEMP
GROUP BY old_classification
ORDER BY count DESC
There are several ways to insert data into a table
One row at a time. This is where you need the values keyword.
Insert into TableA (Col1, Col2,...) values (#Val1, #Val2,...)
You can get the id using select ##identity (at least in ms sql), if you have auto identity
on. This is useful when you need the ID for you next insert.
From select (what you're doing)
Insert into tableA (Col1, Col2)
Select Val1, Val2 from TableB
or insert a hard coded value and values from two separate tables
Insert into tableA (Col1, Col2, Col3, Col4)
Select 'hard coded value', b.Val1, b.Val2, c.Val1
from TableB b join Table c on b.TableCID=c.ID
Now you can insert more than one row at once.
Select into
This method ends up creating a new table from a query, and is great for quick backups of a table, or part of a table.
select * into TermsFinal_backup from TermsFinal where ...
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