mysql merging two tables if not exist - mysql

I want to merge two tables, but not the duplicate entries with similar id field.
But I get error after:
INSERT INTO table1 (id, name)
SELECT id, name FROM table2 WHERE table2.id NOT_IN (SELECT id FROM table1);

You have to change NOT_IN in NOT IN, as this is the correct synthax
INSERT INTO table1 (id, name)
SELECT id, name FROM table2 WHERE table2.id NOT IN(SELECT id FROM table1);

Related

How to insert multiple rows using insert while selecting data from another table

insert INTO table1 (price,service_id,country_id)VALUES(2.00,(SELECT id from table2), (SELECT id from table3))
It shows error.
Table 1-
id, price, service_id, country_id
Table 2-
id, service_name
Table 3-
id, country_name
When inserting in mysql using select query, you don't need the values() anymore.
Try this:
insert INTO table1 (price,service_id,country_id) Select t2.ID, t3.ID from table_2 t2, table_3 t3
Here is one example.

insert all from table1 to table2 having different table structure

table1 have columns id what title and five columns more;
table2 have only columns id what and title;
How to insert all rows from from table2 into table1?
insert into table1 select * from table2 - doesn't work because of different table structure.
You can try below -
insert into table1(id, what, title)
select id, what, title from table2

Mysql 3 tables, copy column

I have three mysql table from same database Db1.
Three tables have following columns.
Table 1:
Name
City
Branch
Table 2:
Age
Address
Country
Table 3:
No columns.
I want to copy Table1.Name and Table2.Age to Table 3. How can I do it?
This makes little sense if table1 and table2 can not be joined and table 3 doesn't have the 2 columns. If you can join:
insert into table3 (name, age)
select table1.name, table2.age
from table1 join table2 on (table1.columnToLinkFromTable1 = table2.columnToLinkFromTable2)
You could also do it like this, but of course it doesn't make much sense:
insert into table3 (name, age)
select table1.name, table2.age
from table1, table2

Grouping data in one table based on links in another table

I have something like this:
table1
ID NAME
table2
ID TABLE1_ID NAME VALUE
table3
ID TABLE2_ID COLLECTION
As you may be able to tell, multiple records from table2 can belong to a record in table1. table3 groups together multiple records in table2
It is no problem for me to loop through and display all of the table2 records for a given table1 record. How can I group those records based on table3 though.
May be by:
select * from table2 where id in (select table2_id from table3);

MySql Duplicate-data an entry on another table

I have table1,table2 => all fields are identical except that table2 two has an extra field which is a FK of table1
**table1** *ID*,content,status
**table2** *ID*,content,status,tid
so tid = table1 id.
I need to copy a row from table1 one to table2 so esentially the table2 would be a backup of table1. I can do it using mysql,then php, then mysql again I was wondering if you could have a simpler solution on mysql :)
hope its not too complicated
If you want to copy every row in table1 into table2, you could do:
INSERT INTO table2 (id, content, status, tid)
SELECT id, content, status, id FROM b;
If table2 isn't empty, you could add an ON DUPLICATE KEY... clause to deal with clashes.
EDIT
If you just want to copy one row, you can add a WHERE clause:
INSERT INTO table2 (id, content, status, tid)
SELECT id, content, status, id FROM b WHERE id=123;
INSERT INTO table2 (content, status, tid) SELECT content, status, ID FROM table1