Merge structurally different MySQL tables on a common value - mysql

I am trying to join 2 tables to a new table based on a shared value, but not having much luck.
Here's what I've got.
Table1:
id |name |
----------|---------|
1 |test1 |
Table2:
id |name |location |color |
----------|---------|---------|---------|
1 | test1 | 10 | blue |
What I'm after here is a new table (Table3) that takes "name" from Table1 and matches it up against name from Table2 and then sticks the matching results into Table3. Anything that doesn't match table 1 should be ignored. So if I had "test99" in Table2, but not in Table1, don't put it in Table3.
Everything I've read says this shouldn't be hard to do, but I'm just not having any luck with it.
Thank you!

Try
SELECT Table2.Name, Table2.Location, Table2.Colour
FROM Table2
INNER JOIN Table1 ON Table2.Name = Table1.Name
Although I noticed you have Id which might be assumed is your Primary Key and Foreign Key ? If so try
SELECT Table2.Name, Table2.Location, Table2.Colour
FROM Table2
INNER JOIN Table1 ON Table2.Id= Table1.Id

You could do a select into table.
Select * INTO Table3
FROM Table2
WHERE Table2.Name IN (SELECT Name FROM Table1)

Or you could use INSERT ... SELECT -syntax and use JOIN.
INSERT INTO table3 (id, name, location, color)
SELECT t.id, t2.name, t.location, t.color FROM table1 AS t JOIN table2 AS t2 ON t2.id = t1.id

Related

Which MySQL JOIN and How...?

I have two tables:
Table 1 contains the User ID
Table 2 contains the user ID and other data I would like
The relationship is on the ID in both tables so what I would like to do is the following:
Pull all data from table 2 where a record exists in the id field in table 2 that matches an id in table 1.
Table 1 has other copies so to speak that are specific to other accounts while table 2 contains all the ids for all the other tables which is why (I think) I need a JOIN statement but I'm open to suggestions.
Table 1:
id
123456
Table 2:
id | name | age
123456 | John | 23
651123 | Mary | 22
811561 | Sarah | 21
You can use subquery as:
SELECT *
FROM table2
WHERE ID IN (SELECT ID
FROM table1)
If you need fields from table1 as well then use an inner join like:
SELECT t1.*, t2.name, t2.age
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.id
Your assumption is correct, you need to join:
Select * from table1 inner join table2 on table1.userid = table2.userid
The only question here is if you want to get only id's that appear on both tables (and than use inner join) or also get such that appear only on the first table as well (left join)
You should choose inner join here because table 2 always contain records for table 1.
You can use INNER JOIN here becuase you have relation between both table, query should be this:
SELECT t1.id, t2.name, t2.age
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id

how to make query show the an other column of the primary key?

umm I'm not sure I've made the title right but its kind of hard to express it in short words.
I have to tables
table1:
id | name
1 | alice
2 | bob
table 2:
user_id | date
2 | 2014-11-1
2 | 2014-11-2
1 | 2014-11-3
as a query, if I want to show table 2 but instead of the integer numbers of user_id, I want it to show the corresponding names of the users where this info is stored in table 1.
I think this is supposed to be easy but I don't know how to get this done.
A query along the lines of -
select t1.name, t2.date
from table_1 t1 inner join table_2 t2 on t1.id = t2.user_id
Try:
SELECT t2.user_id, t1.name
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.user_id
This will do it.
SELECT
`b`.`name`,
`a`.`date`
FROM
table2 a
INNER JOIN table1 b ON (a.user_id = b.id)
SELECT
B.[Name]
,A.[date]
FROM [table 2] A
LEFT OUTER JOIN [table1] B
ON A.[user_id] = B.[id]

Mysql merge w tables with common column

I have:
table1:
Id | Name
table2:
Id | Amount
I want create a new table based on the common Id. So if a record from table1 and table2 have matching id, then:
table3
Id | name | Amount
Sorry if this has been asked before. I'm new to this and just want to get this done
Why wouldn't you do this with a simple select statement?
SELECT a.id, a.Name, b.Amount
FROM table1 a, table2 b
WHERE a.id = b.id
Try using a JOIN
SELECT table1.id, table1.name, table2.amount
FROM table1
LEFT JOIN table2
ON table1.id=table2.id;
I didn't test this but I think it should work.
http://www.w3schools.com/sql/sql_join_left.asp
Like this :
CREATE TABLE tavle_xxx(
id xxx,
name xxx,
amount xxx
);
INSERT IGNORE INTO tavle_xxx
SELECT t1.id, t1.name, t2.Amount
FROM table1 t1, table2 t2
WHERE t1.id=t2.id;
update:
I'd like to merge the tables based on the common column "id" and NOT create a 3rd table. Just add the "name column" and populate it where the id's are =
so i have:
table1:
Id | Name
table2:
Id | Amount
I would like the result to be
table2
Id | Amount | Name
If you want to merge the Name column of table1 in table2, using the common column Id, you can do this by running:
ALTER TABLE table1 ADD Name <dataTypeOfName>;
UPDATE table1, table2 SET table2.Name = table1.Name WHERE table1.Id = table2.Id;
This will create a new column called Name in table2 and then fill it with the values from table1, after performing a join based on the column Id.

Joining 3 SQL tables

I have 3 tables that look like this:
Table1:
PersonSSN
NumberOfCars
Table2:
PersonSSN
NumberOfPhones
Table3:
PersonName
PersonSSN
Both Table 1 and Table2 have a foreign key reference to Table3 on PersonSSN.
I need to join these in such a way that I get:
PersonName NumberOfPhones NumberOfCars
Here are some conditions that apply to the join:
If a person has an entry in both Table1 and Table2 I see all 3 fields populated for him.
If a person has an entry in Table1 and not in Table2 he should still show up but with NumberOfPhones set to 0.
Likewise, if a person has an entry in Table2 and not in Table1 he should still show up but with NumberOfCars set to 0.
Can this be achieved in one query ? If yes what should the query be ?
This is a left outer join query:
select t3.name, coalesce(t1.NumberOfPhones, 0), coalesce(t2.NumberOfCars, 0)
from table3 t3 left outer join
table1 t1
on t3.ssn = t1.ssn left outer join
table2 t2
on t3.ssn = t2.ssn;

mySQL multiple cross reference select query

I'm trying to select the names of products stored in as an ID list in columns a different table. There are 20 columns to lookup so my normal approach would make a huge query. Can someone suggest a simple way to do this?
Table1:
id | productName
Table2
id | p1 | p2 | p3 | p4... up to p20
p1 - p20 each contain an ID number of a product from table1
I need to select rows in table2, replacing p1-p20 with the corresponding productName from table1
Thanks!
Can someone suggest a simple way to do this?
I guess this is a simple way:
SELECT
Table2.id,
T1.productName,
T2.productName,
T3.productName,
-- etc...
FROM Table2
JOIN Table1 T1 ON Table2.p1 = T1.id
JOIN Table2 T2 ON Table2.p1 = T2.id
JOIN Table3 T3 ON Table2.p1 = T3.id
-- etc...
Yes, this is a huge query, but it is simple.
You might want to reconsider your database design. Here's a suggestion:
Table1:
id | productName
Table2
id | index | productid
1 1 p1
1 2 p2
1 3 p3
...etc
Query like this:
SELECT id, index, productName
FROM Table2
JOIN Table1
ON Table2.productid = Table1.id