How to merge two tables with same values in a column - mysql

I have two tables, Table1 and Table2.
Table1 has the columns "ID","Date" and Table2 has the columns "ID", "Cost".
Now, the way I want to merge these two tables into a new table Table3 with columns "ID", "Date", "Cost" is that the Cost and Date are in the same row which have the same ID in both Table2 and Table1 respectively.
In short, I want to glue the two tables with respect to a column, in this case "ID".
I've looked into statements like INSERT INTO TABLE but I haven't been able to get it to work.

You can perform an insert-select on the result of the join between the two source tables:
CREATE TABLE table3 AS
SELECT table1.id AS id, date, cost
FROM table1
JOIN table2 ON table1.id = table2.id

please use below SQL to merge
insert into table3 (id, date1, cost)
select a.id, b.date1, a.cost from table1 a, table2 b where a.id=b.id;

Related

Joining multiple columns in table1 with multiple columns in table2 with additional condition MySql

I have table_1 with unique records of IDs and multiple columns of data
and
table_2 with multiple rows concerning particular ID and multiple columns. One of the column in table2 is, say, time_lapse.
I need those two tables joined with all columns saved but with only those rows from table2 with highest time_lapse value.
I was trying this way...
create table as new_table
select table1.*, table2.* from
table1 left join table2
on table1.id=table2.id
where time_lapse=
(select max(time_lapse) from table2
group by id);
... but it failed.
Any suggestions for a newbie? Thank you.
You are close. But you are selecting the maximum time_lapse per id and then you act as if you had only selected one record with only one time_lapse. Use IN and have the id in the select list of your subquery:
create table as new_table
select table1.*, table2.* from
table1 left join table2
on table1.id=table2.id
where (table2.id, table2.time_lapse) in
(select id, max(time_lapse) from table2
group by id);
Then you are outer-joining table2, but want certain criteria on it in the WHERE clause. That doesn't work (as columns in outer-joined records are null).
The same query a tad prettier with a real outer join:
create table as new_table
select t1.*, t2.*
from table1 t1
left join table2 t2 on t1.id = t2.id
and (t2.id, t2.time_lapse) in
(select id, max(time_lapse) from table2 group by id);

Insert column if 2 column from different table matches

I have 2 tables:
Table1- contains PhoneNumber|Name
Table2- contains PhoneNumber|Address
I want to create Table3 with PhoneNumber|Name|Address
Table1 and Table2 may have out of order or different amount of entries, therefore table1 will act as main list.
please suggest way forward. using MySQL.
Use CREATE..SELECT with a LEFT JOIN :
CREATE TABLE Table3 AS
SELECT t1.PhoneNumber,t1.name,t2.address
FROM Table1 t1
LEFT JOIN Table2 t2
ON(t1.PhoneNumber = t2.PhoneNumber)

Merge two tables to one and remove duplicates

I have 2 tables in the same database.
I want to merge them based on the common id column. Because the tables are too huge I am not sure if there are duplicates.
How is it possible to merge these two tables into one based on the id and be sure that there are no duplicates?
SELECT *
FROM table1,table2
JOIN
GROUP BY id
What do you mean by merging two tables? Do you want records and columns from both the tables or columns from one and records from both?
Either way you will need to change the join clause only.
You could do a join on the columns you wish to
SELECT DISTINCT *
FROM table1 tb1
JOIN table2 tb2
ON table1.id = table2.id
Now if you want columns from only table1 do a LEFT JOIN
If you want columns from only table2 then a RIGHT JOIN
If you want columns from both the tables, use the query as is.
DISTINCT ensures that you get only a single row if there are multiple rows with the same data (but this distinct will check values for all columns in a row whether they are different or the same)
Union won't help if both tables have different number of columns. If you don't know about joins then use a Cartesian product
select distinct *
from table1 tb1, table2 tb2
where tb1.id = tb2.id
Where id is the column that is common between the tables.
Here if you want columns from only table1 do
select distinct tb1.*
Similarly replace tb1 by tb2 in the above statement if you just want table2 columns.
select distinct tb2.*
If you want cols from both just write '*'
In either cases I.e. joins and products said above if you need selective columns just write a table alias. E.g.
Consider :
table1 has id, foo, bar as columns
table2 has id, name,roll no, age
you want only id, foo, name from both the tables in the select query result
do this:
select distinct tb1.id, tb1.foo, tb2.name
from table1 tb1
join table2 tb2
on tb1.id=tb2.id
Same goes for the Cartesian product query. tb1, tb2 are BTW called as a table aliases.
If you want data from both the tables even if they have nothing in common just do
select distinct *
from table1 , table2
Note that this cannot be achieved using a join as join requires a common column to join 'on'
I am not sure What exactly do you want but anyway, this is your code
SELECT *
FROM table1,table2
JOIN
GROUP BY id
i just edit your query
SELECT *
FROM table1 JOIN table2
on table2.id = table1.id
GROUP BY table1.id // here you have to add table
//on which you will be group by at this moment this is table1
Try UNION:
https://dev.mysql.com/doc/refman/5.0/en/union.html
IT is very simple. Hope it will help.
Also you should have a look at "DISTINCT".

Mysql Load two table column value into Single

I have two table in database.
Table1 -> Name
Table2 -> Name
What will be query to get all the "Name" from Table1 and Table2 into single Column.
This query returns the value from the Name column from Table1 and the Name column from Table2, concatenated together into a single resultset.
SELECT t1.Name FROM Table1 t1
UNION ALL
SELECT t2.Name FROM Table2 t2
(This was my understanding of what you were looking for.)
If you want just a "distinct" list of Name values (exclude duplicate occurrences of the same value), then remove the ALL keyword.
If I correctly understood
http://dev.mysql.com/doc/refman/5.0/en/union.html
Select name from table1
union
Select name from table2
You can select data from two tables like this.
SELECT CONCAT(table1.name,table2.name) as Name FROM table1,table2;
and in case table1.name is A and table2.name is b you get
Name = AB
SELECT Name FROM Table1 NATURAL LEFT JOIN Table2 AS t2.
This will give you a list of only non-duplicate names from Table1 and 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