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;
Related
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
Im trying to run this query
("INSERT INTO units SET id="+toID+" AND num="+number+" AND unit_id="+type+"")
this is the log
[2014-05-13 13:09:51] Running query: INSERT INTO units SET id=3 AND num=10 AND unit_id=1
these values are never inserted, it defaults to 0. It only happens on insert queries, i can select fine.
It inserts fine from a php script, however im using Kumulos KScript to run these queries, all have worked fine apart from this one.
Is there a setting on the table that i have missed or something?
Any suggestions?
Many Thanks,
Paul.
INSERT INTO with SET in MYSQL does not need AND as you have
The correct syntax is
INSERT INTO table
SET
col1 = 'val1',
col2 = 'val2',
col3 = 'val3'
OR You can use the following
INSERT INTO table
( col1, col2, col3 )
VALUES
( 'val1', 'val2', 'val3' )
http://dev.mysql.com/doc/refman/5.6/en/insert.html
How about:
INSERT INTO units ( id, num, unit_id ) VALUES ( toID, number, type )
Or:
INSERT INTO units id=toID, num=number, unit_id=type
It is not a correct INSERT query.
Try this:
("INSERT INTO units(id, num, unit_id) VALUES ("+toID+", "+number+", "+type+")")
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
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;