Is it possible to join one row in one table to multiple rows in another table with SQL Server?
SweetCheeks
2009-02-12 13:19:30 UTC
I have a table (Table A) where I want to JOIN each row with multiple rows in another table (Table B) with SQL. Is this possible? If so, how can I do this.
If not, is there another way to do this without creating duplicates in Table A?
Thanks so much!! ^^
Three answers:
Ryan J
2009-02-12 13:28:00 UTC
It can be done with a join. Just use a where clause on the left table.
ie:
SELECT Employee.Username, Project.ProjectName
FROM Employee
LEFT JOIN Project ON Employee.EmployeeID = Project.EmployeeID
where Employee.Username = "JaneDoe";
John Michael
2009-02-12 21:27:37 UTC
What what what?
"I want to JOIN each row with multiple rows" What does that mean?
Do you mean a row with multiple columns?
You can take information out of an individual row (by a key identifier) and insert it into another table. Sure.
To check for dupes, you can search for a value, say email address. I'm not sure what would be best because you didn't give us any specifics on your DB.
JMK
http://realjobdescriptions.com
2009-02-12 21:25:57 UTC
Consider the following table structure:
Table_A (myId, Name)
Table_B(a_id, Child)
Table_A may have more than one child, linked by myId to a_id. The query then looks like this:
SELECT a.myId, a.Name, b.child FROM Table_A a
INNER JOIN Table_B b
ON a.myId = b.a_id
WHERE a.myId = @id
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.