473,566 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Table join question

I'm using ASP.NET and SQL Server and this might be an obviuos question for
most, but if I have a table that contains several fields that I need to
relate to just one field in another table, how do I do that?

I.e. Table 1 has Integer values for enteredbyID, CoordID, CustContactID
Table 2 is the contacts table with Contact_ID (integer) key. Table 1 uses
that field to bring back First and Last Name's.

So:
Table1 Table 2
enteredbyID Contact_ID
CoordID Contact_ID
CustContactID Contact_ID

I'm fine if I link one field in Table 1 to Table 2.

I can't make join from All Table 1 fields to Contact_ID field??

thanx.
Nov 19 '05 #1
7 1702
Hi Chris,

Here's the thing: A table (result set) has the same number of rows as
records, and the same number of columns per record as the number of columns
fetched in the query. So, you're not going to be able to eliminate the
duplicate ContactIDs in your result set. HOWEVER, that doesn't mean you have
to DISPLAY them all.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Chris" <Ch***@discussi ons.microsoft.c om> wrote in message
news:7E******** *************** ***********@mic rosoft.com...
I'm using ASP.NET and SQL Server and this might be an obviuos question for
most, but if I have a table that contains several fields that I need to
relate to just one field in another table, how do I do that?

I.e. Table 1 has Integer values for enteredbyID, CoordID, CustContactID
Table 2 is the contacts table with Contact_ID (integer) key. Table 1 uses
that field to bring back First and Last Name's.

So:
Table1 Table 2
enteredbyID Contact_ID
CoordID Contact_ID
CustContactID Contact_ID

I'm fine if I link one field in Table 1 to Table 2.

I can't make join from All Table 1 fields to Contact_ID field??

thanx.

Nov 19 '05 #2
pj
On Fri, 4 Mar 2005 09:21:05 -0800, "Chris"
<Ch***@discussi ons.microsoft.c om> wrote:
I'm using ASP.NET and SQL Server and this might be an obviuos question for
most, but if I have a table that contains several fields that I need to
relate to just one field in another table, how do I do that?

I.e. Table 1 has Integer values for enteredbyID, CoordID, CustContactID
Table 2 is the contacts table with Contact_ID (integer) key. Table 1 uses
that field to bring back First and Last Name's.

So:
Table1 Table 2
enteredbyID Contact_ID
CoordID Contact_ID
CustContactI D Contact_ID

I'm fine if I link one field in Table 1 to Table 2.

I can't make join from All Table 1 fields to Contact_ID field??


I'm not quite sure I understand your question properly...

if you have:

table1:
enteredbyID
CoordID
CustContactID
and table2:
ContactID
FirstName
LastName

where table1.CustCont actID refers to table2.ContactI D

then you can use a query to join the tables e.g.

SELECT table1.*, table2.FirstNam e, table2.LastName FROM table1
INNER JOIN table2 ON table1.CustCont actID = table2.ContactI D

which will give you:

datatable:
enteredbyID
CoordID
CustContactID
FirstName
LastName

which you can bind to your datagrid.

is that what you're after or have I misunderstood?

pj

--
http://www.psychedelicjones.com/
Nov 19 '05 #3
The issue is that I need to create a grid where I display the Names of the
people not the Integter ID for three fields. So the only table that I have
the Name in is the Contacts table related to those integter value. I can join
for one field (i.e EnteredBY) but the other two go wtihout. I wrote a
function to handle this on the pages, i.e. I send the integer value to gte
back the Full Name. But I'm wondering if I can do this here in the Grid. I
using (showing for one working Name):

<asp:TemplateCo lumn HeaderText="Ass igned To"
HeaderStyle-CssClass="gridq text" ItemStyle-CssClass="gridt ext">
<ItemTemplate >
<asp:Label id="lblassigned to" width="150" runat="server" Text='<%#
DataBinder.Eval (Container.Data Item, "FullName") %>' Visible="True">
</asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>

I'm wondering if there is a simple way to use my function here in the grid:
qc2005s.GetName (Person_ID value)

Maybe in the OnCreate for the Grid? But how?

So, via the table themselves is out I take it.

Thanx.
"Kevin Spencer" wrote:
Hi Chris,

Here's the thing: A table (result set) has the same number of rows as
records, and the same number of columns per record as the number of columns
fetched in the query. So, you're not going to be able to eliminate the
duplicate ContactIDs in your result set. HOWEVER, that doesn't mean you have
to DISPLAY them all.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Chris" <Ch***@discussi ons.microsoft.c om> wrote in message
news:7E******** *************** ***********@mic rosoft.com...
I'm using ASP.NET and SQL Server and this might be an obviuos question for
most, but if I have a table that contains several fields that I need to
relate to just one field in another table, how do I do that?

I.e. Table 1 has Integer values for enteredbyID, CoordID, CustContactID
Table 2 is the contacts table with Contact_ID (integer) key. Table 1 uses
that field to bring back First and Last Name's.

So:
Table1 Table 2
enteredbyID Contact_ID
CoordID Contact_ID
CustContactID Contact_ID

I'm fine if I link one field in Table 1 to Table 2.

I can't make join from All Table 1 fields to Contact_ID field??

thanx.


Nov 19 '05 #4
you got it but I need to do all three at once. I.e. bring back the fullname
for each of Table 1's integer values. Table 1 has three fields that I need to
convert in the grid to fullname, via a view if possible (see reply to this).

thanx.

"pj" wrote:
On Fri, 4 Mar 2005 09:21:05 -0800, "Chris"
<Ch***@discussi ons.microsoft.c om> wrote:
I'm using ASP.NET and SQL Server and this might be an obviuos question for
most, but if I have a table that contains several fields that I need to
relate to just one field in another table, how do I do that?

I.e. Table 1 has Integer values for enteredbyID, CoordID, CustContactID
Table 2 is the contacts table with Contact_ID (integer) key. Table 1 uses
that field to bring back First and Last Name's.

So:
Table1 Table 2
enteredbyID Contact_ID
CoordID Contact_ID
CustContactI D Contact_ID

I'm fine if I link one field in Table 1 to Table 2.

I can't make join from All Table 1 fields to Contact_ID field??


I'm not quite sure I understand your question properly...

if you have:

table1:
enteredbyID
CoordID
CustContactID
and table2:
ContactID
FirstName
LastName

where table1.CustCont actID refers to table2.ContactI D

then you can use a query to join the tables e.g.

SELECT table1.*, table2.FirstNam e, table2.LastName FROM table1
INNER JOIN table2 ON table1.CustCont actID = table2.ContactI D

which will give you:

datatable:
enteredbyID
CoordID
CustContactID
FirstName
LastName

which you can bind to your datagrid.

is that what you're after or have I misunderstood?

pj

--
http://www.psychedelicjones.com/

Nov 19 '05 #5
pj
On Fri, 4 Mar 2005 10:02:24 -0800, "Chris"
<Ch***@discussi ons.microsoft.c om> wrote:
The issue is that I need to create a grid where I display the Names of the
people not the Integter ID for three fields. So the only table that I have
the Name in is the Contacts table related to those integter value. I can join
for one field (i.e EnteredBY) but the other two go wtihout. I wrote a
function to handle this on the pages, i.e. I send the integer value to gte
back the Full Name. But I'm wondering if I can do this here in the Grid. I
using (showing for one working Name):

<asp:TemplateC olumn HeaderText="Ass igned To"
HeaderStyle-CssClass="gridq text" ItemStyle-CssClass="gridt ext">
<ItemTemplat e>
<asp:Label id="lblassigned to" width="150" runat="server" Text='<%#
DataBinder.Eva l(Container.Dat aItem, "FullName") %>' Visible="True">
</asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>

I'm wondering if there is a simple way to use my function here in the grid:
qc2005s.GetNam e(Person_ID value)

Maybe in the OnCreate for the Grid? But how?

So, via the table themselves is out I take it.


ok, I think I understand what you mean. let me try again:

table1:enteredb yID:CoordID:Cus tContactID
row1: 1: 2: 3

table2: ContactID: FirstName: SecondName
row1: 1 : John : Smith
row2: 2 : Susan : Scott
row3: 3: Mickey : Mouse
Your query becomes:

SELECT
EnteredBy.First Name AS EnteredByFirstN ame,
EnteredBy.LastN ame AS EnteredByLastNa me,
Coord.FirstName AS CoordFirstName,
Coord.LastName AS CoordByLastName ,
CustContact.Fir stName AS CustContactFirs tName,
CustContact.Las tName AS CustContactLast Name
FROM table1 AS T
INNER JOIN table2 AS EnteredBy ON EnteredBy.Conta ctID = T.enteredbyID
INNER JOIN table2 AS Coord ON Coord.ContactID = T.coordID
INNER JOIN table2 AS CustContact ON CustContact.Con tactID =
T.CusrtcontactI D

and your results set just has the names you need.

pj
--
http://www.psychedelicjones.com/
Nov 19 '05 #6
Wait a minute. I'm a little confused. I just looked at your table layout
from your first message, and I have a couple of questions:
> Table1 Table 2
> enteredbyID Contact_ID
> CoordID Contact_ID
> CustContactID Contact_ID

At first I thought this was a result set with 2 columns (yeah, I know, I
should have looked closer). But as I reconsider it, it looks like 2 tables,
all the columns of table1 and one column from Table2, repeated 3 times. That
was what confused me: the repetition of the column in the second table. In
fact, don't your 2 tables look more like the following?

Table1 Table 2
------------------------------
enteredbyID Contact_ID
CoordID
CustContactID

Now, assuming that you have a name for each Table2 Contact_ID, you would
indeed need a JOIN to ge t the name, and you wouldn't need a function to do
it. That is:

Table1 Table 2
------------------------------
enteredbyID Contact_ID
CoordID Contact_Name
CustContactID

In which case, you can execute the following SQL to get it:

SELECT Table1.enteredb yID AS enteredByID, Table1.CoordID AS CoordID,
Table2.Contact_ Name AS Contact_Name
FROM Table1 INNER JOIN Table2 ON Table1.CustCont actID = Table2.Contact_ ID

This query would result in soomething ike the following result set:

enteredByID CoordID Contact_Name
1 123 John Doe
2 234 John Smith
3 345 Jane Doe

you could then bind that to your DataGrid without any DataBinding
expressions.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Chris" <Ch***@discussi ons.microsoft.c om> wrote in message
news:F2******** *************** ***********@mic rosoft.com... The issue is that I need to create a grid where I display the Names of the
people not the Integter ID for three fields. So the only table that I have
the Name in is the Contacts table related to those integter value. I can
join
for one field (i.e EnteredBY) but the other two go wtihout. I wrote a
function to handle this on the pages, i.e. I send the integer value to gte
back the Full Name. But I'm wondering if I can do this here in the Grid. I
using (showing for one working Name):

<asp:TemplateCo lumn HeaderText="Ass igned To"
HeaderStyle-CssClass="gridq text" ItemStyle-CssClass="gridt ext">
<ItemTemplate >
<asp:Label id="lblassigned to" width="150" runat="server" Text='<%#
DataBinder.Eval (Container.Data Item, "FullName") %>' Visible="True">
</asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>

I'm wondering if there is a simple way to use my function here in the
grid:
qc2005s.GetName (Person_ID value)

Maybe in the OnCreate for the Grid? But how?

So, via the table themselves is out I take it.

Thanx.
"Kevin Spencer" wrote:
Hi Chris,

Here's the thing: A table (result set) has the same number of rows as
records, and the same number of columns per record as the number of
columns
fetched in the query. So, you're not going to be able to eliminate the
duplicate ContactIDs in your result set. HOWEVER, that doesn't mean you
have
to DISPLAY them all.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Chris" <Ch***@discussi ons.microsoft.c om> wrote in message
news:7E******** *************** ***********@mic rosoft.com...
> I'm using ASP.NET and SQL Server and this might be an obviuos question
> for
> most, but if I have a table that contains several fields that I need to
> relate to just one field in another table, how do I do that?
>
> I.e. Table 1 has Integer values for enteredbyID, CoordID, CustContactID
> Table 2 is the contacts table with Contact_ID (integer) key. Table 1
> uses
> that field to bring back First and Last Name's.
>
> So:
> Table1 Table 2
> enteredbyID Contact_ID
> CoordID Contact_ID
> CustContactID Contact_ID
>
> I'm fine if I link one field in Table 1 to Table 2.
>
> I can't make join from All Table 1 fields to Contact_ID field??
>
> thanx.


Nov 19 '05 #7
Thank you both for your input. I was able to integrate my function to change
the intergers (names) to first and last names. I did so in the ItemCreate sub
of the datagrid build:

e.Item.Cells(9) .Text = qc2005s.GetName (enteredby)

I'll try your SQL approach when I have a less complex sql string. I couldn't
do it here given the time, this view has 7 interelated tables :)

Thanx again.

"pj" wrote:
On Fri, 4 Mar 2005 09:21:05 -0800, "Chris"
<Ch***@discussi ons.microsoft.c om> wrote:
I'm using ASP.NET and SQL Server and this might be an obviuos question for
most, but if I have a table that contains several fields that I need to
relate to just one field in another table, how do I do that?

I.e. Table 1 has Integer values for enteredbyID, CoordID, CustContactID
Table 2 is the contacts table with Contact_ID (integer) key. Table 1 uses
that field to bring back First and Last Name's.

So:
Table1 Table 2
enteredbyID Contact_ID
CoordID Contact_ID
CustContactI D Contact_ID

I'm fine if I link one field in Table 1 to Table 2.

I can't make join from All Table 1 fields to Contact_ID field??


I'm not quite sure I understand your question properly...

if you have:

table1:
enteredbyID
CoordID
CustContactID
and table2:
ContactID
FirstName
LastName

where table1.CustCont actID refers to table2.ContactI D

then you can use a query to join the tables e.g.

SELECT table1.*, table2.FirstNam e, table2.LastName FROM table1
INNER JOIN table2 ON table1.CustCont actID = table2.ContactI D

which will give you:

datatable:
enteredbyID
CoordID
CustContactID
FirstName
LastName

which you can bind to your datagrid.

is that what you're after or have I misunderstood?

pj

--
http://www.psychedelicjones.com/

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3055
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me solve my problem. I'll turn to MySQL doc after getting through this pressing project. Thanks a lot Roger! Babale -----Urspr=FCngliche...
2
2651
by: terence.parker | last post by:
I am often faced with the dilemma of whether to use a JOIN query across three tables in order to grab a bunch of results - or whether to create another table to represent what I want. The latter is less normalised, but seems less computationally expensive to me(?). Basicially what I have is: Friend (uid1,uid2,fid) ->...
3
1577
by: Jack Smith | last post by:
Hello, I want to be able to view data from 3 tables using the JOIN statement, but I'm not sure of how to do it. I think i don't know the syntax of the joins.I imagine this is easy for the experienced - but Im not. Allow me to explain: I have 2 Tables: PERSON and SIGN PERSON
2
8514
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An example of what I'm trying to do is below: update (tbl_ind_mananas LEFT JOIN (select count(*) as count, (dubicacion || zona || manzana) as cod_manzana...
15
2704
by: Hi5 | last post by:
Hi, I am designing a database for a client in which It has a client table including the followings: 1-Table Client 2-Table lookupcategory 3-Table Ctegory
3
2028
by: das | last post by:
Hello all, Can someone help me with this SQL? 1) EMPLOYEE table has columns 'employee_id' and 'emp_sid' and other columns 2) EMPLOYEE_BENEFITS table has a column called employee_entity, this column can be joined to either 'employee_id' OR 'emp_sid' but not both in the EMPLOYEE table.
2
2029
by: mmitchell_houston | last post by:
I'm working on a .NET project and I need a single query to return a result set from three related tables in Access 2003, and I'm having trouble getting the results I want. The details: Question ------------ QuestionID QuestionText Question_MediaTypeID
5
1544
by: Regnab | last post by:
I have a table "tblSprayApplication" in a project for a nursery. Each record refers to a single spray application across the nursery, recording which groups had been sprayed. Multiple applications (in different parts of the greenhouse) can occur targetting the same set of pests. My question is how should I best record this? Originally I was...
5
1807
by: perryche | last post by:
I am not sure if I am asking the right question in the subject here. But, here is my problem. I have 5tables/queries with various data like below. Table1: CustomerID1, Field1, Field2 Table2: CustomerID1, Field5, Field6 Table3: CustomerID2, Field7, Field8 (customerID1 does not exist in the table) ....
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.