If I have two tables with following columns:
Table1: [id,value]
Table2: [id,comment]
where id and value are numeric and comment is a string.
I need to retrieve all the ids with value>50 in the Table1.
And make an
INSERT IGNORE INTO table2 VALUES (id,"higher than 50");
for each one of those ids. How can I make it in MySQL? Thanks
INSERT IGNORE INTO table2
SELECT id,"higher than 50"
FROM Table1 where value > 50;
Related
I have 2 SQL tables, table1 and table2.
table1 has two columns and I want to insert values to these columns.
one of the columns should get a static value and the other column should get a value that is a result of a query from table2.
If I wanted to insert the static data separately I would do:
INSERT INTO table1(login_id)
VALUES ('1234');
and if I wanted to insert the dynamic value separately I would do:
INSERT INTO table1(user_uuid)
SELECT users_uuid FROM table2 where first_name like 'ortal';
How can insert both values to table1 in one action?
If I try the first query I get:
11:20:45 INSERT INTO table1(login_id ,user_uuid) VALUES ('1234') Error Code: 1136. Column count doesn't match value count at row 1 0.000 sec
INSERT INTO `users`.`table1` (`login_id`) VALUES ('1234');
ERROR 1364: 1364: Field 'user_uuid' doesn't have a default value
You can add the constants to your select list and treat them a columns:
INSERT INTO table1(user_uuid, login_id)
SELECT users_uuid, '1234' FROM table2 WHERE first_name LIKE 'ortal';
i got 2 tables that i want to combine its data.
the id is my key field (incremental and distinct).
table 1 and table 2 field description for example:
id - name - value
i want to insert all of table 2 data into table 1, they have different data but in some rows the same id.
so when i try to:
INSERT INTO ladatabase.table2 SELECT * from ladatabase.table1;
i get duplicate entry error.
please help me to suggest how can i solve it and combine the data with different id values for the new data.
Alex.
here are 2 ways:
first you can use if the id AUTO INCREMENT
INSERT INTO ladatabase.table2
SELECT '', name, value from ladatabase.table1;
or you find the first free ID and add it
INSERT INTO ladatabase.table2
SELECT id+555 , name, value from ladatabase.table1;
No insert all fields (exclude 'id'-field from SELECT):
INSERT INTO ladatabase.table2 SELECT name,value from ladatabase.table1;
You need to copy all but the ID column, and it will assign new IDs. You need to list all the other columns explicitly. Leaving out the ID column will cause it to get its default value, which are sequential IDs for an auto_increment field.
INSERT INTO table2 (col2, col3, col4, ...)
SELECT col2, col3, col4, ...
FROM table1
How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.
Im trying to use insert and select in one query.Both the tables have same number of columns except for one column in the table where data is to be inserted
table2 is the mirror image of table1 except for a column called comments;
insert into table2 select * from table1 where city='XYZ' and name = 'STY'
since the no of columns arent equal i get the following error:
Column count doesn't match value count at row 1
INSERT INTO table2 (Coln1,Coln2,Coln3,....) SELECT * FROM table1 WHERE `city`='XYZ' AND `name`='STY';
For the extra column in the table2, NULL is set by default.
Sometimes if I want to quickly copy records from one table to another (that has the same structure) I use a query like this:
INSERT INTO table2 SELECT * FROM
table1 WHERE id = SOME_VALUE
How can I add a ON DUPLICATE KEY UPDATE to this statement? I tried this:
INSERT INTO SELECT * FROM table1 WHERE
id = 1 ON DUPLICATE KEY UPDATE SELECT
* FROM table1 WHERE id = 1
But I get an error. Is there away to accomplish the query above with out individually listing each column in the query?
P.S. Yes, I realize that it is not good practice to have multiple tables with identical structures, but sometimes you just don't get control over everything in the workplace!
The below UPDATES if there is no PK duplication and INSERTs is there is:
REPLACE INTO table2(field1, field2, field3)
SELECT field1, field2,field3 FROM table1
WHERE id=1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html Just use the SELECT field_name from the other table like in dnagirls example