Insert values into table from multiple sources - mysql

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

Related

How to insert values into a table according to the index values of columns in MySQL database?

I want to add values according to index in my table.
This is the code I am trying right now.
insert into A(1,2) values ('ABC','CBA');
The insert statement does not accepts column indexes, but column names. Say your table called a has three columns called id, col1 and col2, and you want to insert into the last two columns, you would do:
insert into a (col1, col2) values('ABC', 'CBA');
The only way that you can add columns according to position is to leave out the column list and include all columns:
insert into A
values ('ABC', 'CBA');
That said, you really should be explicit about which columns are getting values, by including the column names:
insert into A (col1, col2)
values ('ABC', 'CBA');
Or using the MySQL set extension:
insert into A (col1, col2)
set col1 = 'ABC', col2 = 'CBA';

SQL insert list of values in one column dynamicallly

I would like to execute a insert query once for inserting multiple list of records in one column
INSERT INTO Table (col1, col2, col3)
VALUES (val1, val2, listVal3);
The third column only is the list
listVal3 is a list of ids from request
Is it possible to execute to a query like above to insert multiple records
in one column dynamically, if so please help me, thanks.
Maybe you want to create several records having the same values in the first two columns and taking the third column values from table request? In that case the following statement might be useful:
INSERT INTO Table (col1, col2, col3)
SELECT 'val1', 'val2', id from request
WHERE ... --- (some conditions)
You can concat the listVal3 values.
INSERT INTO `tmp_tbl2` (col1, col2, col3)
VALUES (val1, val2, (SELECT GROUP_CONCAT(id) FROM request WHERE ....))

Select Insert Query by mentioning all columns across databases

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

max value of no autonincrement column in update query

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;

INSERT result of a SELECT and other values as well

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;