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 ....))
Related
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';
I am trying to find a solution to update or insert value in my MySQL table (like in firebird you have UPDATE OR INSERT INTO TABLE (COL1, COL2, COL3) VALUES (VAL1, VAL2, VAL3) MATCHING (COL1, COL2).
I know MySQL have on duplicate key but I do not want that! I do not want to set a key on some of my columns because that one table is used depending on the situation.
So is there any other way of performing update or insert action?
if I remember correctly COL1 should be unique
INSERT INTO table
(COL1, COL2, COL3)
VALUES
(?, ?, ?)
ON DUPLICATE KEY UPDATE
COL2 = VALUES(COL2)
COL2 = VALUES(COL2 )
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
How can I at an insert query, check if a specific column has a specific value and then update the row. Otherwise it should insert a new row.
Somethings like this is what I want:
INSERT INTO table (col1, col2, col3) VALUES (val1, val2, val3) UPDATE IF col3 = $number
Create a UNIQUE key over your col3:
ALTER TABLE `table` ADD UNIQUE KEY (col3)
Then use INSERT ... ON DUPLICATE KEY UPDATE:
INSERT INTO `table` (col1, col2, col3) VALUES (val1, val2, val3)
ON DUPLICATE KEY UPDATE col1 = VALUES(col1), col2 = VALUES(col2)
Have a look at the REPLACE INTO command (MySQL docu).
REPLACE INTO table (col1, col2, col3) VALUES (val1, val2, val3);
It checks primary keys and unique indexes and when there is a matching entry already present, that row is replaced by the new one, else a new row is inserted.
EDIT
As #eggyal mentioned this the behaviour in the case, when the row is replaced, is actually a deletion of the old row and insertion of the new row afterwards. This may lead to some problems, when you're using triggers, foreign keys or alike.
Actually you might loose the content of some columns as well. Suppose you have a table with 3 columns (col1 to col3), but the REPLACE just sets two of them (col1, col2), the third one col3 will receive the default value specified and not retain the old value.
if you have unique index on that column then use this
INSERT INTO table (col1, col2, col3) VALUES (val1, val2, val3)
ON DUPLICATE KEY UPDATE col3 = $number
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;