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;
Related
In the LOAD DATA documentation, there's a SET syntax which allows values to be assigned to column.
...
SET col=#variable;
...
I was wondering if the SET can support assignment from 2 columns from a single table, I can't find anything in the documentation that supports tuple assignment:
...
SET (col1, col2) = (SELECT col1, col2 from table where id=1);
...
Anyone have any knowledge on this? Thank you!
Set doesn't work like this, however I know that in TSQL at least you can do the same thing using select.
Select #var1 = col1, #var2= col2
from table
I want to insert data into a 3 columns of a table. 2 columns are from other table and the third value is an array variable. I need to process this within a loop. Can anyone help ?
I want something like below,
Insert into table_name (col1,col2,col3) values ((Select a,b from Source_table),array_variable(i));
Is that possible?
Insert into table_name (col1, col2, col3)
Select a, b, 'static value'
from Source_table
I am working on redesigning of a legacy db and I have set new names to columns of old db. So, for instance, if olddb.oldtable under dbold has column descr, I have set it as description in new newdb.netable for column.
How can I mention individual columns in my query?
I am using MYSQL
Update: Both Databases are on different IP Addresses and I am using Navicat to transfer data.
You can try like this:
INSERT INTO newtable (col1, col2, ..., )
SELECT col1, col2, ..., FROM oldtable
By trying the above query you can insert the specific column. So for example if your newtable has a column as description and old table as descr then you can mention it like:
INSERT INTO newtable (col1, col2, `description`, ..., )
SELECT col1, col2, `descr` ,..., FROM oldtable
Also if the table column list is large and you want to copy all the columns and its data then you can simply use the wildcard charater * as:
INSERT INTO newtable
SELECT * FROM oldtable;
You can insert all columns at once without the need to mention the names using this:
INSERT INTO newtable (SELECT * FROM oldtable);
It will make an 1x1 match independently of column names.
If types don't match then will insert default values (not checked for all the type combination).
Note that column number must be the same on both tables otherwise an error like this will occur:
#1136 - Column count doesn't match value count at row 1
Is it possible to retrieve the maximum value of a key column, increase it and insert it as a new value in the database, like this (pseudo-sql)
insert into mytable (mykeycolumn,col1,col2)
values((max(mykeycolumn)+1),'val1','val2');
Yes, you INSERT from a SELECT, but your fixed fields must "come from the SELECT" as well, like such:
INSERT INTO mytable (mykeycolumn, col1, col2)
SELECT MAX(mykeycolumn)+1, 'val1', 'val2'
FROM mytable;
Complementing: as a_horse_with_no_name pointed out, MAX() + 1 could cause your problems if you have simultaneous transactions in mytable. At some point two identical mykeycolumn values would be generated and you would get an error. The ideal solution is to convert your table to use auto_increment.
You can with this:
INSERT INTO mytable (mykeycolumn,col1,col2) VALUES
((SELECT MAX(mykeycolumn) FROM mytable AS foo)+1,'val1','val2');
try this
INSERT INTO mytable (mykeycolumn, col1, col2)
SELECT SCOPE_IDENTITY()+1, 'val1', 'val2' FROM mytable;
I have a question about the multi-ing (bulk) insert with mysql..
I know that:
INSERT INTO "my_table" ('col1','col2','col3') VALUES
(1,1,1),(2,2,2);
But I need to do something like:
INSERT INTO "my_table" ('col1','col2','col3') VALUES
((SELECT select1 as col1,select2 as col3 from "my_table2"),"textForAllCol2")
Where my select return a list of 2 column.
I'M trying to explain the best i can..
Thanks for your help!
JP
Here is referenced SQLFiddle for you
Modify your query as
INSERT INTO my_table SELECT col1, "textForAllCol2", col2 FROM my_table2;
You can use it like this
INSERT INTO "my_table" ('col1','col2','col3')
SELECT select1, "textForAllCol2", select2 from my_table2
try this
INSERT INTO my_table (`col1`,`col2`,`col3`)
SELECT select1, "textForAllCol2", select2 from my_table2
If your source and destination columns names are related (even partially) with each other, I prefer to use aliases for readability:
INSERT INTO my_table (`col1`,`col2`,`col3`)
SELECT `col1`, "textForAllCol2" as `col2`, `select3` as `col3` from my_table2