Click here to Skip to main content
15,566,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help as I just started learning SQL Server. I have this issue in appending the "required" columns from 4 different tables using SQL server and the problem here is that I want to connect the table even with a duplicate column name and display it all.

Database name is hower:

table 1: dep = dp, den

table 2: section = id, sec

table 4: users = id, fame, lame, deptID, section yearlevel

table 5: year= id, string

What I have tried:

I've tried doing right join, but I can only join two tables together with its column, but the data in each column had become null.
Posted
Updated 3-Apr-22 11:00am
v4
Comments
0x01AA 3-Apr-22 11:54am    

1 solution

You can't join tables unless there is a relationship between them: they need to "share" information between them.

And there is nothing in your tables that is recognisably common data, except for a "deptID" in "table 4" which doesn't exist in any of the other three tables!

You JOIN tables when they do have a relationship: for example:
Invoices Table
ID
InvNumber
InvDate
CustID

InvoicceLines Table
ID
InvoiceID
Quantity
ProductID
PricePerUnit

Then to pick up the whole invoice, you use a JOIN:
SQL
SELECT i.InvNumber, il.*
FROM Invoices i
JOIN InvoiceLines il
  ON il.InvID = i.ID
WHERE i.InvNumber = 666

But if there is no relationship, there is nothing to JOIN!
 
Share this answer
 
Comments
Maciej Los 3-Apr-22 15:09pm    
Short And To The Point!
Member 15356357 4-Apr-22 3:17am    
is there any other way to connect them?
OriginalGriff 4-Apr-22 3:52am    
Not unless there is some "common factor" - and we would have no idea what that was!

Think about it: how would you connect a list of pets and their names, ages, and preferred food with a list of cars that used a specific car park and their make, model, colour, and which parking space they used when?

You can't - because there is no common data, and nobody has any idea what a relationship might be. The two datasets may be related if the car park is exclusively used for pet store customers, but you and I have no idea if it is!

So think about your data and what it represents - identify what information relationships there are, and then you can start thinking about linking them.

Sorry, but we can't help you with that at all!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900