1

I have 2 tables, both with an ID and notes, these IDs and notes need blending, I do not know where to start with this, here is an example of what I have

TBL_1
ID    Info
1     Comment 1
2     Comment 2
3     Comment 3
4     Comment 4

TBL_2
ID    Info
2     Comment 5
4     Comment 6

and ideally what I would like is a query or a table that looks like this

ID    Info1        Info 2
1     Comment 1
2     Comment 2    Comment 5
3     Comment 3
4     Comment 4    Comment 6

Any sort of help or solution would be very appreciated

Thanks

P.s I messed around creating a union query using this code in SQL

SELECT TBL_1.[ID], TBL1.Info
FROM TBL_1
UNION ALL SELECT [TBL_2].ID, [TBL_2].[Info]
FROM TBL_1 INNER JOIN [TBL_2] ON TBL_1.[ID] = [TBL_2].ID;
Tom H
  • 13
  • 3
  • Please show what you have already tried and where you got stuck, so that people can help you... – skirato Jun 08 '15 at 08:36
  • Apologies if the sample data doesn't show up properly, its basically an ID column, and a Info column in two tables and the result I need is ID and Info column 1 and info column 2 ( so there is only one ID but the second tables notes are in info 2) – Tom H Jun 08 '15 at 08:36
  • I tried a union Query, but this just left me with 2 columns where the ID has duplicates and the notes are all unique in the second column @skirato – Tom H Jun 08 '15 at 08:37
  • Can you update your question with the code you tried? – skirato Jun 08 '15 at 08:37
  • @skirato ive added what I tried to achieve – Tom H Jun 08 '15 at 08:45

1 Answers1

1

You don't need union, you need an outer join. Try this:

SELECT TBL_1.[ID], TBL_1.info, TBL_2.info As Info2
FROM TBL_1 
LEFT JOIN Tbl_2 ON(Tbl_1.[ID] = Tbl_2.[ID])

See fiddle here

Read here about different join types and when to use them.

Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Thank you for your answer, this seems to just come back with an error "syntax error in join operation" – Tom H Jun 08 '15 at 09:44
  • Sorry, There was a period instead of a comma between TBL1.Info and TBL2.Info. Try now – Zohar Peled Jun 08 '15 at 09:45
  • im very sorry but this is still showing the same syntax error – Tom H Jun 08 '15 at 09:55
  • AH sorted it, was a daft spelling mistake on my behalf, this worked slightly, the second column of info is blank – Tom H Jun 08 '15 at 09:58
  • I've had some confusion with Tbl_1 and Tbl1, also Tbl2 instead of Tbl_2. I've Sorted it out and updated my answer. – Zohar Peled Jun 08 '15 at 10:02
  • im very sorry to keep hassling, but it still doesn't seem to work @zohar – Tom H Jun 08 '15 at 10:33
  • Did you check the fiddle I've linked to? it's exactly what you described as your sample data. Please update the question to include the relevant tables DDL statements (right click on the table in SSMS, choose script table as -> create -> to new query and copy the result to your question.) – Zohar Peled Jun 08 '15 at 10:43
  • PERFECT! this works an absolute treat, thank you so much! @zohar – Tom H Jun 08 '15 at 11:02