I have two tables:
vote('id', 'question_id', 'ip_id')
and
ip('id','ip_addr')
I want to do something like this:
INSERT INTO SELECT `vote`.`question_id`, `ip`.`ip_addr`
FROM `vote`
LEFT JOIN `ip`
ON `vote`.`ip_id` = `ip`.`id` VALUES '2','127.0.0.1'
the above code is not working, any idea?
The syntax of INSERT INTO SELECT is like this:
INSERT INTO Table2
(Column1, Column3)
SELECT Column1, Column3
FROM Table1
So your query should be like this:
INSERT INTO <TableName> (`question_id`, `ip_addr`)
SELECT `vote`.`question_id`, `ip`.`ip_addr`
FROM `vote`
LEFT JOIN `ip`
ON `vote`.`ip_id` = `ip`.`id`
You can insert rows into an existing table using one of the following methods:
INSERT INTO vote (id, question_id, ip_id)
VALUES (....)
OR
INSERT INTO vote (id, question_id, ip_id)
SELECT id, question_id, ip_id
FROM
<some existing tables, optionally using joins, where clauses etc., to select the data to insert>
In your case, you seem to be trying to insert record in to the IP table as well as the VOTE table at the same time and that is not possible.
You should first insert a record in the IP table using the first syntax above and then insert into VOTE table using the second option.
Related
I'm trying to write a query that extracts and transforms data from a table and then insert those data into another table. Yes, this is a data warehousing query and I'm doing it in MS Access. So basically I want some query like this:
INSERT INTO Table2(LongIntColumn2, CurrencyColumn2) VALUES
(SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1);
I tried but get a syntax error message.
What would you do if you want to do this?
No "VALUES", no parenthesis:
INSERT INTO Table2(LongIntColumn2, CurrencyColumn2)
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1;
You have two syntax options:
Option 1
CREATE TABLE Table1 (
id int identity(1, 1) not null,
LongIntColumn1 int,
CurrencyColumn money
)
CREATE TABLE Table2 (
id int identity(1, 1) not null,
LongIntColumn2 int,
CurrencyColumn2 money
)
INSERT INTO Table1 VALUES(12, 12.00)
INSERT INTO Table1 VALUES(11, 13.00)
INSERT INTO Table2
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1
Option 2
CREATE TABLE Table1 (
id int identity(1, 1) not null,
LongIntColumn1 int,
CurrencyColumn money
)
INSERT INTO Table1 VALUES(12, 12.00)
INSERT INTO Table1 VALUES(11, 13.00)
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1
INTO Table2
FROM Table1
GROUP BY LongIntColumn1
Bear in mind that Option 2 will create a table with only the columns on the projection (those on the SELECT).
Remove both VALUES and the parenthesis.
INSERT INTO Table2 (LongIntColumn2, CurrencyColumn2)
SELECT LongIntColumn1, Avg(CurrencyColumn) FROM Table1 GROUP BY LongIntColumn1
I believe your problem in this instance is the "values" keyword. You use the "values" keyword when you are inserting only one row of data. For inserting the results of a select, you don't need it.
Also, you really don't need the parentheses around the select statement.
From msdn:
Multiple-record append query:
INSERT INTO target [(field1[, field2[, …]])] [IN externaldatabase]
SELECT [source.]field1[, field2[, …]
FROM tableexpression
Single-record append query:
INSERT INTO target [(field1[, field2[, …]])]
VALUES (value1[, value2[, …])
Remove VALUES from your SQL.
Remove "values" when you're appending a group of rows, and remove the extra parentheses. You can avoid the circular reference by using an alias for avg(CurrencyColumn) (as you did in your example) or by not using an alias at all.
If the column names are the same in both tables, your query would be like this:
INSERT INTO Table2 (LongIntColumn, Junk)
SELECT LongIntColumn, avg(CurrencyColumn) as CurrencyColumn1
FROM Table1
GROUP BY LongIntColumn;
And it would work without an alias:
INSERT INTO Table2 (LongIntColumn, Junk)
SELECT LongIntColumn, avg(CurrencyColumn)
FROM Table1
GROUP BY LongIntColumn;
Well I think the best way would be (will be?) to define 2 recordsets and use them as an intermediate between the 2 tables.
Open both recordsets
Extract the data from the first table (SELECT blablabla)
Update 2nd recordset with data available in the first recordset (either by adding new records or updating existing records
Close both recordsets
This method is particularly interesting if you plan to update tables from different databases (ie each recordset can have its own connection ...)
inserting data form one table to another table in different DATABASE
insert into DocTypeGroup
Select DocGrp_Id,DocGrp_SubId,DocGrp_GroupName,DocGrp_PM,DocGrp_DocType
from Opendatasource( 'SQLOLEDB','Data Source=10.132.20.19;UserID=sa;Password=gchaturthi').dbIPFMCI.dbo.DocTypeGroup
Do you want to insert extraction in an existing table?
If it does not matter then you can try the below query:
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 INTO T1 FROM Table1
GROUP BY LongIntColumn1);
It will create a new table -> T1 with the extracted information
ive done this to pull the distinct data
INSERT INTO `goac`.`customer`
( CUST_ID)
SELECT DISTINCT CUST_ID
FROM ods_customer
but I would like to bring the rest of the data with it and have it fill out the table.
I am pulling data from 2 tables for this one.
According to your info above. I guess...
Try to add more column in insert statement
INSERT INTO table_name VALUES (value1, value2, value3...);
Maybe like
INSERT INTO `goac`.`customer` ( CUST_ID, CUST_NM, CUST_INCOME_AM, CUST_STREET_AD...) SELECT DISTINCT CUST_ID FROM ods_customer
I want to make an sql query which can insert into table1. This table references table2 with the table2_id foreign key.
my sql looks like this so far:
INSERT INTO table1 (table1_id, name, date, table2_id)
VALUES (1, "somename", "29.04.2014", (SELECT id FROM table 2 WHERE table2.name = "BOB") )
I also want to insert values into table2 if it cannot find the table2.name and then insert the key for this into table1.
Anyone knows how to do this?
I would suggest that you use insert . . . select rather than insert . . . values:
INSERT INTO hovedenhet (organisasjonsnummer, navn, stiftelsesdato, registreringsdatoEnhetsregisteret, organisasjonsform_id)
SELECT 813550202, 'SAMEIET SCHWEIGAARDSGATE 21-23', '10.01.2014', '29.04.2014', id
FROM organisasjonsformhovedenhet oh
WHERE oh.organisasjonsform = 'BOB';
Your original query will insert the row with a NULL value for the last column, if there is no match. This will not insert anything in that case.
select cons_id,teh_id,local_id,panchayt_id,war_id,ha_id from b...;
select rep_value_id from val;`
i need to get the above values into single column in another table.
how can i solve it by a query or using stored procedure.
You can do something like this in oracle sql .
Insert into VAL_TABLE (ID,COMMON_FIELD) values (VAL_TABLE_ID.nextval, ( SELECT CONS_ID || TECH_ID || LOCAL_ID from TABLE_B));
Or in MySQL
Insert into VAL_TABLE (ID,COMMON_FIELD) values (1, ( SELECT concat( id, type, details) from TABLE_B ) );
Try this...
UPDATE my_table SET col1=
(SELECT CONCAT_WS(',',val.rep_value_id,cons_id,teh_id,local_id,panchayt_id,war_id,ha_id)
FROM b,val);
This question already has answers here:
How to do INSERT into a table records extracted from another table
(9 answers)
Closed 3 years ago.
As the title says, I am trying to insert into one table selecting values from another table and some default values.
INSERT INTO def (catid, title, page, publish)
(SELECT catid, title from abc),'page','yes')
INSERT INTO def (catid, title, page, publish)
VALUES
((SELECT catid, title from abc),'page','yes'))
The first query gives a mysql error and the second one gives column count does not match.
What do I need to do?
You simply have to do:
INSERT INTO def (catid, title, page, publish)
SELECT catid, title, 'page','yes' from `abc`
If you want to insert all the columns then
insert into def select * from abc;
here the number of columns in def should be equal to abc.
if you want to insert the subsets of columns then
insert into def (col1,col2, col3 ) select scol1,scol2,scol3 from abc;
if you want to insert some hardcorded values then
insert into def (col1, col2,col3) select 'hardcoded value',scol2, scol3 from abc;
INSERT INTO def (field_1, field_2, field3)
VALUES
('$field_1', (SELECT id_user from user_table where name = 'jhon'), '$field3')
If you you want to copy a sub-set of the source table you can do:
INSERT INTO def (field_1, field_2, field3)
SELECT other_field_1, other_field_2, other_field_3 from `abc`
or to copy ALL fields from the source table to destination table you can do more simply:
INSERT INTO def
SELECT * from `abc`
With MySQL if you are inserting into a table that has a auto increment primary key and you want to use a built-in MySQL function such as NOW() then you can do something like this:
INSERT INTO course_payment
SELECT NULL, order_id, payment_gateway, total_amt, charge_amt, refund_amt, NOW()
FROM orders ORDER BY order_id DESC LIMIT 10;