You are viewing limited content. For full access, please sign in.

Question

Question

[Lookup] Populate table column from multiple columns in database table

asked on October 9, 2015

If I need to fill a drop-down in the form with data from two or more different columns from a database table, can it be done?

 

e.g. I have three columns in the database with three different names, and I want those three names, in one drop-down as three distinct entires 

0 0

Answer

SELECTED ANSWER
replied on October 9, 2015

So what you need is view in your sql server that does a join. 

 

This page should help you with a high level over view of a basic join. http://www.w3schools.com/sql/sql_join.asp

 

The hard part will be finding a common piece of data between the tables to associated each entry. I'd also suggest wrapping your join statement in another select statement so that you can do a SELECT DISTINCT and get rid of any duplicates. 

 

This is an example:

 

SELECT DISTINCT *
FROM (
SELECT 
	
	[Table1].Column1 as CO1, 
        [Table2].Column1 as CO2,
        [Table2].Column2 as CO3


	FROM [Table1]
        LEFT JOIN [Table2]

        On [Table1].MatchingColumn = [Table2].MatchingColumn

	) O
	ORDER BY CO1, CO2

The reason I enclose it all is so that I can get rid of any duplicates that might occur and I also do an Order By simply for ease of use when looking at the view. 

 

Once you've tested that you can insert your query you can then insert it into a view, instructions for that are here:  http://www.w3schools.com/sql/sql_view.asp

This view is what you will actually query. 

You will want to make sure you don't get too many results with your queries. A left join will return all rows with column 1 plus all columns that match (as determined by the ON clause). If there is a column1 that doesn't match it will return a NULL (which will be interpreted as blank) on the other columns. 

Be careful as it can make for some very large queries!

1 0
replied on October 12, 2015

Thanks for the comprehensive reply, Chris. I suspected a view is the only way out, will work on it now.

0 0

Replies

replied on October 9, 2015

So do you want a 3 column lookup, or all three in one column?

 

i.e 

 

Column 1------Column2-------Column3

C1.1                  C2.1                   C3.1
C1.2                  C2.2                   C3.2
C1.3                  C2.3                   C3.3

 

So do you want it so that if you choose C1.1 as field 1, it will fill in Filed 2 and 3 with the associated row.

 

Or do you want all of those in one big long table?

C1.1

C1.2

C1.3

C2.1

C2.2

etc...

 

 

 

 

1 0
replied on October 9, 2015

Chris,

 

Thanks for replying.

 

If I select C1.1 in a forms drop-down, another drop-down will get filled with C2.1 and  C3.1 as options in it.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.