How do I join 2 tables that have all of the same column headers but one?
The other 3 columns are all the same then one table has the column header "January" and the other table has the column header "February"
So I would like to end up with 5 total columns in new table
I would also like all of the data in there even if there are duplicates. Before I used a union all but now I cannot do that because not all my headers are the same.
Thanks!
You can use UNION and rename one of your columns:
SELECT id, name, age, xxx FROM table1
UNION
SELECT id, name, age, yyy AS xxx FROM table2
If you add more information, I can elaborate my answer
select tb1.col1, tb1.col2, tb1.col3, tb2.col3 from tb1
inner join tb2 on (tb1.col1 = tb2.col1)
Assuming uniqueness when just considering the three columns, joining them
SELECT tab1.col1,
tab1.col2,
tab1.col3,
tab1.january,
tab2.february
FROM tab1,
tab2
WHERE tab1.col1 = tab2.col1
AND tab1.col2 = tab2.col2
AND tab1.col3 = tab2.col3
This assumes all rows in tblA have corresponding ones in tblB and vice versa:
SELECT * FROM tblA INNER JOIN tblB USING (col1, col2, col3);
If that is not the case, and depending on the data (if tbl1.january can have legitimate null values, this won't work), you could try something like (didn't check for sure, might need massaged with parenthesis, subselecting, etc...):
SELECT *
FROM tbl1
LEFT JOIN tbl2 USING (col1, col2, col3)
UNION ALL
SELECT * FROM tbl2 LEFT JOIN tbl1 USING (col1, col2, col3)
HAVING tbl1.`january` IS NULL
;
Related
For question purposes I will use minimal data for my examples.
I have a table called table1 and a column named test that looks like this:
test
5012
I am able to add an extra zero behind before the result of column test using this query:
SELECT CONCAT('0',test) as x from table1
this is the result of the query:
results table: table1
x
05012
Now I have another table called table2 looking like this:
test test2
05012 1
My question is, how do I join the two tables together based on that query above and concat the table1 with column test2 from table2? Making sure the first 4 characters of both columns test from both tables match together?
This is how table 1 should look like:
Afterquery
050121
I am curious why you wouldn't simply use table2?
select concat(t2.test, t2.test2) as afterquery
from table2 t2;
table1 doesn't seem to play a role.
If you want values in table2 filtered by table1, you can use exists:
select concat(t2.test, t2.test2) as afterquery
from table2 t2
where exists (select 1
from table1 t1
where t2.test = concat('0', t1.test)
);
You can express this as a join:
select concat(t2.test, t2.test2) as afterquery
from table2 t2 join
table1 t1
on t2.test = concat('0', t1.test);
This is useful if you want columns from both tables -- but that is not necessary to answer the question. On the other hand, this runs the risk of duplication if there are multiple matches.
I think that this should be the solution. You need to use concat in the join between table1 and table2
SELECT CONCAT('0', table1.test, table2.test2) AS Afterquery
FROM table1
INNER JOIN table2
ON CONCAT('0',table1.test) = table2.test
Slightly different approach with a sub-query:
select concat(concat_test, test2) test_results
from
(select concat('0', test) concat_test from table1) table_alias
join
table2 on substring(concat_test,1,4) = substring(test,1,4);
I am getting data from a linked server, in which lets say I am having an identifier which I am joining with the identifier on my server and getting the data for it.
But the thing is identifier from Linked server contains some extra characters and while joining its not able to correctly join.
Can i do something like this that when I join, I will replace the identifies extra characters with space or NULL.
For ex.
Table 1
Col1 Col2
23rf name
24rf id
Table 2
Col1 Col2
23 name1
24 id1
SELECT
ta1.*,
ta2.*
FROM Table1 ta1
INNER JOIN Table2 ta2
ON ta1.Col1 = ta2.Col2
So this will give NULL
I want a query which can result the data by replacing "rf" to "" and join the two table, so that I can't get a NULL DataSet.
Second Approach:
Can I insert the data from one table to another table where while inserting, I can replace "rf" to "".
But, I do not know how to proceed for the above approach.
Please suggest.
You can do this by assigning an id to each of your tables using ROW_NUMBER. So, I used now row as their common field. See and try my queries below:
In SQL-SERVER:
SELECT ta1.Col1,ta1.Col2,ta2.Col1,ta2.Col2 FROM
(SELECT
ROW_NUMBER() OVER(ORDER BY Col1) AS Row,*
FROM Table1) AS ta1
INNER JOIN
(SELECT
ROW_NUMBER() OVER(ORDER BY Col1) AS Row,*
FROM Table2) AS ta2
ON ta1.Row=ta2.Row
In MYSQL:
SELECT ta1.Col1,ta1.Col2,ta2.Col1,ta2.Col2 FROM
(SELECT
#row_number1:=#row_number1+1 AS RowNumber1,
Col1,
Col2
FROM Table1, (SELECT #row_number1:=0)AS x ORDER BY Col1) AS ta1
INNER JOIN
(SELECT
#row_number2:=#row_number2+1 AS RowNumber2,
Col1,
Col2
FROM Table2, (SELECT #row_number2:=0)AS y ORDER BY Col1) AS ta2
ON ta1.RowNumber1=ta2.RowNumber2
Clearly, I am not a SQL guy, so I have to ask for help on the following rather simple task.
I have two SQL Server 2008 tables: t1 and t2 with many identical columns and a key column (entry_ID). T2 has rows that do not exist in t1 but should.
I want to merge those rows from t2 that do not exist in t1 but I also do not want any rows from t2 that already exist in t1. I would like the result set to fill a new t3.
I have looked at many solutions online but can't find the solution to the above scenario.
Thank you.
There are a number of ways to do it you could use UNION ALL or OUTER JOIN.
Assuming you are using Entry_ID to find identical records, and Entry_ID is unique within each table, here is a OUTER JOIN method:
This gets you your recordset: T1 and T2 merged:
SELECT
CASE
WHEN T1.Entry_ID IS NULL THEN 'T2'
WHEN T2.Entry_ID IS NULL THEN 'T1'
ELSE 'Both'
END SourceTable,
COALESCE(T1.Entry_ID,T2.Entry_ID) As Entry_ID,
COALESCE(T1.Col1, T2.Col1) As Col1,
COALESCE(T1.Col2, T2.Col2) As Col2,
COALESCE(T1.Col3, T2.Col3) As Col3,
COALESCE(T1.Col4, T2.Col4) As Col4
FROM T1 FULL OUTER JOIN T2
ON T1.Entry_DI = T2.Entry_ID
ORDER BY COALESCE(T1.Entry_DI,T2.Entry_ID)
This inserts it into T3:
INSERT INTO T3 (Entry_ID,Col1, COl2,Col3,Col4)
SELECT
COALESCE(T1.Entry_DI,T2.Entry_ID) As Entry_ID,
COALESCE(T1.Col1, T2.Col1) As Col1,
COALESCE(T1.Col2, T2.Col2) As Col2,
COALESCE(T1.Col3, T2.Col3) As Col3,
COALESCE(T1.Col4, T2.Col4) As Col4
FROM T1 FULL OUTER JOIN T2
ON T1.Entry_DI = T2.Entry_ID
Again you must note that Entry_ID needs to be unique within their tables, and it uses this to match between the tables.
Also note the columns from the select line up with the column list in the insert statement - the order of the columns in the physical table doesn't matter, the INSERT and SELECT just have to line up.
I have to make a table out of two other tables (and use union). The query that works is:
SELECT * FROM tabel1
UNION
SELECT * FROM tabel2
Now what i have to do is put this result(data) into table3 (a table i already have, with the same columns as in table1 and table2).
Who can help me?
INSERT INTO table3
SELECT * FROM tabel1
UNION
SELECT * FROM tabel2
since you have the same columns in all three of them ...
In a general case you should work with column lists like
INSERT INTO table3 (col1, col2, col3)
SELECT col1, col2, col3 FROM tabel1
UNION
SELECT col1, col2, col3 FROM tabel2
This way you avoid trouble with auto_increment id-columns. Also You should consider using UNION ALL since UNION filters out duplicate lines and therefore will take longer on large tables.
I have two tables Table1 and Table2. There are 10 fields in Table1 and 9 fields in Table2. There is one common column in both the tables i.e. AdateTime. This column saves unix time stamp of user actions. I want to display records from both the tables as a single result but sorting must me according to AdateTime. Recent action should be display first. Sometimes many recent actions in Table1 but few in Table2. Vice versa is also possible. So I want to fetch combine result set from both the tables using single query. I am using PHP MySQL.
Try
SELECT t1.*, t2.*
FROM table1 t1 INNER JOIN table2 t2
ON t1.AdateTime = t2.AdateTime
ORDER BY t1.AdateTime
or (if tables are not related)
SELECT * FROM
(SELECT ADateTime, col1, col2, col3, col4 FROM table1
UNION
SELECT ADateTime, col1, col2, 1 AS col3, NULL AS col4 FROM table2) t2
ORDER by ADateTime
I would use UNION ALL with an inline view. So something like
select col1,col2,col3,col4,col5,col6,col7,col8,col9,AdateTime
from
(
select col1,col2,col3,col4,col5,col6,col7,col8,col9,AdateTime from Table1
UNION ALL
select col1,col2,col3,col4,col5,col6,col7,col8,null as col9,AdateTime from Table2
) t
order by t.Adatetime desc;
yes you can do it. you just need to join these 2 tables with a join condition. when the join condition matches for a row only that row ll be displayed then further you can write the Code for any operation. use order by AdateTime
select t1.column_1234,t2.column_1234
from t1 table1 , t2 table2
where t1.matching_column = t2.matching_column
order by t1.AdateTime;
t1.matching_column And t2.matching_column are the Primary And Foreign keys for these tables (Matching Column)