I'm trying to copy a line from a table to another table. But the second table have to received two additional parameters (hour and autor) which are not in the first table.
Here is my try:
INSERT INTO Individus_corbeille
(
SELECT *
FROM Individus
WHERE ID_individu='706782','NOW()','autor'
)
But it absolutely doesn't work...
INSERT INTO Individus_corbeille
SELECT *, NOW(), 'autor'
FROM Individus
WHERE ID_individu='706782'
But you should actually also name the columns you insert into and the ones you select like this
INSERT INTO Individus_corbeille (col1, col2, col3, col4)
SELECT col1, col2, NOW(), 'autor'
FROM Individus
WHERE ID_individu='706782'
Related
I intend to perform the following in just one sql request
INSERT INTO TABLE1 (col2,col3,col4) VALUES('a','b',current_insert_id());
But current_insert_id() seems to be not implemented.
Attempts:
INSERT INTO TABLE1 (col2, col3, col4) VALUES('a','b',last_insert_id());
it gives me 0 on the last field
INSERT INTO TABLE1 (col2, col3, col4) VALUES('a','b','dummy_value');
UPDATE TABLE1 SET col4=LAST_INSERT_ID() WHERE ID=LAST_INSERT_ID();
but this uses 2 queries.
One way to do this would be:
INSERT INTO TABLE1
(col2,col3,col4)
VALUES
(
'a',
'b',
(
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "YOUR_DB_NAME"
AND TABLE_NAME = "TABLE1"
)
);
Don't do it in production though. It's not nice :)
I'm trying to duplicate a row in a mysql db and at the same time replace part of a string in one field of the duplicated row.
I've figured out how to:
duplicate a row
INSERT INTO account_external_ids
SELECT * FROM account_external_ids
where external_id like '%gerrit:%';
do the replace of the string but cant figure out how to do both in the same query.
UPDATE account_external_ids
SET external_id = REPLACE(external_id,'gerrit:','username:')
WHERE external_id like '%gerrit%';
But can't figure out how to do both in the same query, something like:
INSERT INTO account_external_id
select * from account_external_ids
set external_id = replace(external_id, 'gerrit:', 'username:')
where external_id like '%gerrit%';
Any pointers would be great.
thanks
You have to list all the columns explicitly, you can't use SELECT * if you're modifying any of the columns
INSERT INTO account_external_id (col1, col2, col3, col4, external_id)
SELECT col1, col2, col3, col4, REPLACE(external_id, 'gerrit:', 'username:')
FROM account_external_ids
WHERE external_id LIKE '%gerritt:%'
This question already has answers here:
INSERT INTO ... SELECT without detailing all columns
(11 answers)
Closed 8 years ago.
I have a syntax that can copy all records from tables that have the same fields:
INSERT INTO table2 SELECT * FROM table1 WHERE id = '7'
So I would like to know how do I keep copying all values except the 'id' field (because him is primary key and auto increment).
You list the columns:
insert into table2(col1, . . . coln)
select col1, . . ., coln
from table1
where id = '7';
Under most circumstances, you should list the columns explicitly. You can run into problems even when tables have the same columns but in a different order.
(The one exception in code that I would write is in a script where table1 is created from table2 using create table as.)
You have to list all the columns specifically:
INSERT INTO table2 (col1, col2, col3, ...)
SELECT col1, col2, col3, ...
FROM table1
WHERE id = '7'
You'd need to specify the fields you're copying explicitly:
INSERT INTO table2 (col1, col2, col3, etc)
SELECT col1, col2, col3, etc
FROM table1
WHERE id = '7'
I have two tables,
tblA(id, num, col1, col2, col3),
tblB(col1, col2, col3)
col1, col2 and col3 are the same in both tables. Now I have following sql:
declare #num...(same type as num)
insert into tblA
select #num, * from tblB
id in tblA is an indentity column.
But I got following error,
Column name or number of supplied values does not match table definition.
Can anyone help me to fix it?
Can you please try providing the column names as well,
declare #num...(same type as num)
insert into tblA(num, col1, col2, col3)
select #num, * from tblB
Please don't worry about identity column as it will get filled automatically.
Just INSERT using named columns, and skip the identity column - it will be filled automatically:
INSERT INTO tblA (num, col1, col2, col3) SELECT #Num, col1, col2, col3 FROM tblB
I think the error message is quite explicative: the SELECT and the INSERT has to have the same number of columns.
in your case
declare #num...(same type as num)
insert into tblA(num,col1, col2, col3)
select #num,col1, col2, col3 from tblB
if the key on tblA is not auto-generated you have to consider it in the INSERT
more info here
It simply based on your column name they should be of same type:
insert into tblA(col1,col2,col3)
select col1,col2,col3
from tblB
I want to know if this is possible without a procedure or server side calls into the database.
I am trying to insert values into a table based on a select, and other values that will be provided from the server.
The select statement will return more than one result.
I am aware of the existence of INSERT SELECT, but is there any SELECT INSERT ? or a way to insert based on the results of a select ?
thank you
Not really sure what seems to be the problem.
You can do like this:
INSERT INTO table (columns)
SELECT
column or column expression1,
column or column expression2,
…
constant or constant expression1,
constant or constant expression2,
…
FROM a set of tables/joins
WHERE …
Not necessarily in that order (columns, then constants), no. You can mix columns with constants any way you like, just follow the order of the columns you are inserting into.
Was that what you were asking about?
I don't see why an
INSERT INTO yourtable(col1, col2, col3)
SELECT col1, col2, col3
FROM yourothertable
doesn't work for you. But you could always do a SELECT INTO #temptable to save your query in a temporary table and then you could INSERT that data or manipulate it prior to inserting. This is just a long way around the original idea, though.
Am I misunderstanding your questions?
Yes. Use this query:
INSERT INTO FOO (oof, rab) SELECT (foo, bar) FROM BAR;
I think you can do this:
INSERT INTO targetTable (col1, col2, col3)
SELECT col1, col2, col3 FROM sourceTable
UNION ALL
SELECT 'something' AS col1, 'something else' AS col2, 'yet something else' AS col3 FROM DUAL;