How do I merge two tables in SQL? - mysql

I've got two tables:
Table 1 = idhash - username - usermail
Table 2 = idhash - views - fistseen - lastseen
Now I want to merge these tables to a new table:
Table 3 = idhash - username - usermail - firstseen - lastseen
*notice that I want to drop the views column.
I've tried with solutions that I found on google, but so far they do not seem to work.
Not all the idhash columns from table 2 have a corresponding idhash in table 1. Stiil store those 'mismatched' rows with empty username and usermail

This query will give you the results:
SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash
And to insert and update the results on your [Table 3]:
INSERT INTO [Table 3](idhash, username, usermail, firstseen, lastseen)
SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash
LEFT JOIN [Table 3] C
ON A.idhash = C.idhash
WHERE C.idhash IS NULL
-- For SQL Server
UPDATE T3
SET firstseen = T1.firstseen,
lastseen = T1.lastseen
FROM [Table 3] T3
INNER JOIN (SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash) T1
WHERE T3.firstseen != T1.firstseen OR T3.lastseen != T1.lastseen

Here's a solution for MySQL:
CREATE TABLE table3
// First get all the rows from table2, paired with matching rows from table1
(SELECT a.idhash, b.username, b.usermail, a.firstseen, a.lastseen
FROM table2 a
LEFT JOIN table1 b
ON b.idhash = a.idhash)
// Now get the remaining rows from table1 that don't have matches
UNION ALL
(SELECT null, a.username, a.usermail, null, null
FROM table1 a
LEFT JOIN table2 b
ON b.idhash = a.idhash
WHERE b.idhash IS NULL)
If you don't want the rows from table1 that don't have corresponding rows in table2, then delete the second query in the UNION clause.

You could just brute force it with your choice of programming language. Just build a new table, query both tables, join rows programmatically however they need to be joined, insert into new table.

insert into table3
select t1.idhash, t1.username, t1.usermail, t2.firstseen,t2.lastseen
from table1 t1 left join table2 t2 on t1.idhas=t2.idhas

This should be a good start. You need to tell us what to do with your mis-matched records before we can give anything more specific.
Select
table1.idhash
,username
,usermail
,firstseen
,lastseen
From table1
left join table2
on table1.idhash = table2.idhash

Create your new table with the field types in your create statement
http://www.w3schools.com/sql/sql_create_table.asp
Then do an insert Into Table3 <yourtable> select a.f1,b.f2,c.f3 from Table1 a, Table 2 b on a.id = b.id

This is pretty close to brute force.
select coalesce(t1.idhash, t2.idhash) as idhash
, username
, usermail
, firstseen
, lastseen
into table3
from table1 t1
cross join table2 t2

try this:
INSERT INTO table3 (idhash, username, usermail, firstseen, lastseen)
SELECT a.idhash, a.username, a.usermail,
b.firstseen, b.lastseen
FROM table1 a LEFT JOIN table2 b
ON a.idhash = b.idhash

I would do this:
CREATE TABLE table3
AS
SELECT idhash, username, usermail, firstseen, lastseen
FROM Table1
NATURAL FULL OUTER JOIN Table2

Related

MYSQL - How do I create a single query to accomplish this

I have a fairly complex situation where I'd like to see if there is a single query I can use to get the data.
In the following pseudo code, assume the "*" in each select clause has the appropriate columns selected and that table3 and table4 have similar enough columns that I can handle the diffs with padding/fake columns.
select * from table1 where ActiveFlag=1
for each row returned (as A)
//check if member has a record in membership table
select * from table2 where MembershipId=A.MembershipId
if (row exists)
select * from table3 where MemberId=A.MemberId
else
select * from table4 where MembershipId=A.MembershipId
end of for loop
This assumne each row in TableA can have 0-1 membership, otherwise we need to filter duplicates first.
SELECT CASE WHEN M.MembershipId IS NULL
THEN T4.field1
ELSE T3.field1
END as field1,
....
CASE WHEN M.MembershipId IS NULL
THEN T4.fieldN
ELSE T3.fieldN
END as fieldN
FROM TableA A
LEFT JOIN Membership M
ON A.MembershipId = M.MembershipId
CROSS JOIN table3 T3
CROSS JOIN table4 T4
WHERE A.MemberId = T3.MemberId
OR A.MembershipId = T4.MembershipId
If you have duplicate Membership replace the first two JOIN with something like this.
FROM (
SELECT DISTINCT TableA.*, M.MembershipId
FROM TableA A
LEFT JOIN Membership M
ON A.MembershipId = M.MembershipId
) A
CROSS JOIN ..

joining tables with where clause

I am having two tables, the structure is given below
Table 1
schid
name
cost
type
Table 2
schid
details
oldcost
I am unable to write a query to display records from table 2 of let suppose type A OR B (Here as you can see type field is in table 1), Here one more thing to add is that schid is not a primary key, The query which i am executing is retrieving more records than expected, I think due to join, Can i execute it without using join
SELECT *
FROM Table1
JOIN Table2 ON Table1.schid=Table2.schid
WHERE Table1.type='A'
OR Table1.type='B'
This would help:
SELECT t2.schid, t2.details, t2.oldcost
FROM Table2 t2
JOIN Table1 t1
ON t1.schid = t2.schid
WHERE t1.type IN ('A', 'B');
This should retrieve only the table 2 records which match the criteria.
SELECT t2.*
FROM Table2 t2
JOIN Table1 t1 ON t1.schid = t2.schid
WHERE t1.type = 'A'
OR t1.type = 'B';
SELECT t2.*
FROM `Table2` t2
JOIN `Table1` t1 ON t2.`schid`=t1.`schid`
WHERE t1.`type` IN ('A','B');

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;

3 tables and 2 left joins

Query 1:
SELECT sum(total_revenue_usd)
FROM table1 c
WHERE c.irt1_search_campaign_id IN (
SELECT assign_id
FROM table2 ga
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
)
Query 2:
SELECT sum(total_revenue_usd)
FROM table1 c
LEFT JOIN table2 ga
ON c.irt1_search_campaign_id = ga.assign_id
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
Query 1 gives me the correct result where as I need it in the second style without using 'in'. However Query 2 doesn't give the same result.
How can I change the first query without using 'in' ?
The reason being is that the small query is part of a much larger query, there are other conditions that won't work with 'in'
You could try something along the lines of
SELECT sum(total_revenue_usd)
FROM table1 c
JOIN
(
SELECT DISTINCT ga.assign_id
FROM table2 ga
JOIN table3 d
ON d.campaign_id = ga.assign_id
) x
ON c.irt1_search_campaign_id = x.assign_id
The queries do very different things:
The first query sums the total_revenue_usd from table1 where irt1_search_campaign_id exists in table2 as assign_id. (The outer join to table3 is absolutely unnecessary, by the way, because it doesn't change wether a table2.assign_id exists or not.) As you look for existence in table2, you can of course replace IN with EXISTS.
The second query gets you combinations of table1, table2 and table3. So, in case there are two records in table2 for an entry in table1 and three records in table3 for each of the two table2 records, you will get six records for the one table1 record. Thus you sum its total_revenue_usd sixfold. This is not what you want. Don't join table1 with the other tables.
EDIT: Here is the query using an exists clause. As mentioned, outer joining table3 doesn't alter the results.
Select sum(total_revenue_usd)
from table1 c
where exists
(
select *
from table2 ga
-- left join table3 d on d.campaign_id = ga.assign_id
where ga.assign_id = c.irt1_search_campaign_id
);

MySQL - Join two tables, different columns in table 1 on the same column in table 2

I'm trying to join a different column (part_type_n (where n ranges from 1 to 54)) on Table1 with the same column (id, primary, autoinc) on Table2.
Schema:
Table1
==============
part_type_1
.
.
.
part_type_54
Table2
=============
id
I tried the obvious query (php generated, looping through n from 1 to 54), omitted repetitive stuff in ...:
SELECT * FROM Table1 JOIN Table2 on (Table1.part_type_1=Table2.id), ..., (Table1.part_type_54=Table2.id)
I receive this error:
1066 - Not unique table/alias: 'Table2'
How do I join these two tables?
You will have to join the table on it self again multiple times.
SELECT * FROM table1 t1
INNER JOIN table2 t2 on t2.Id=t1.part_type_1
INNER JOIN table2 t3 on t3.id = t1.part_type_54;
Hope this helps!
As an alternative to writing a query with 54 table aliases, you could consider joining to the table once - like so:
select ...
from Table1 t1
join Table2 t2
on t2.id in (t1.part_type_1, t1.part_type_2, ... t1.part_type_54)
It worked for me to get my required result as one row of which matches various categories all stored in one table column.
Query
SELECT cm3.*, xp.post_title,GROUP_CONCAT(DISTINCT sc.name) AS cate_list
FROM `xld_posts` xp
JOIN course_map cm0 ON cm0.course_id = xp.ID
JOIN course_map cm1 ON cm1.course_id = cm0.course_id AND cm0.id = 3
JOIN course_map cm2 ON cm2.course_id = cm1.course_id AND cm1.id = 6
JOIN course_map cm3 ON cm3.course_id = cm2.course_id AND cm2.id = 11
JOIN subject_category sc ON cm3.id = sc.id
GROUP by post_title ORDER BY post_title
Note: the categories values 3, 6, and 7 are got from form sumbit. Thus if your form has more than three or less your query should dynamically created and join each table with previous table.
:) Happy if any one felt useful.