Click here to Skip to main content
16,017,248 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
I have a column named LEADER in 16 tables in my sql database I'd like to join all the tables to show the Leader column and the table it belongs to in datagridview.

What I have tried:

SqlCommand cmd = new SqlCommand("Select GroupLeaders.ID,Account.Leader, Engineers.Leader from GroupLeaders inner join Account on GroupLeaders.id = Account.Leader", conn);
Posted
Updated 6-Feb-23 0:36am
v2
Comments
Andre Oosthuizen 6-Feb-23 6:18am    
Read more on the different jopin statements that you can use to achieve your goal on multiple tables - https://learnsql.com/cookbook/how-to-join-multiple-3-plus-tables-in-one-statement/
Dave Kreskowiak 6-Feb-23 8:42am    
Why does it sound like you have 16 tables with essentially the same data in all of them? This sounds like a very badly designed database.
PIEBALDconsult 6-Feb-23 11:12am    
Or Billionth Normal Form.

1 solution

Try something like this:
SQL
SELECT a.LEADER AS [A Leader]
      ,b.LEADER AS [B Leader]
      ,c.LEADER AS [C Leader]
FROM TableA a
JOIN TableB b ON a.ID = b.ID
JOIN TableC c ON a.ID = c.ID

And also read up here: SQL Joins[^]
 
Share this answer
 
v2
Comments
Maciej Los 7-Feb-23 11:36am    
5ed!

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