Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
desperatemeasures
Starting Member
4 Posts |
Posted - 2010-03-09 : 15:55:32
|
Hi All, Sorry to bother you with a simple request, but I am trying to teach myself SQL, and at the moment the Join clause.I have set myself a scenario: I have 2 tables, one called EmployeeDetails (for employee usernames, DOB, addresses etc.) and the other called OfficeDetails (for their office name and location)I have set the tables out as follows:Table: EmployeeDetailsEmployeeNumUsernameDateOfBirthAddressTable: OfficeDetailsEmployeeNumOfficeNameOfficeLocationIf I were wanting to display the OfficeName for a particular Username (entered through a VB.NET form textbox), could I use: SELECT OfficeName FROM OfficeDetails WHERE “Username.Text” = Username AND EmployeeNum.EmployeeDetails = EmployeeNum.OfficeDetailsOr would I have to do 2 separate statements?Any help would be gratefully received! |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2010-03-09 : 16:05:51
|
You need to do a join between tables OfficeDetails and EmployeeDetails.Your current statement won't work because it only uses table OfficeDetails, but is selecting on column Username which is in table EmployeeDetails.CODO ERGO SUM |
|
|
desperatemeasures
Starting Member
4 Posts |
Posted - 2010-03-09 : 16:17:06
|
quote: Originally posted by Michael Valentine Jones You need to do a join between tables OfficeDetails and EmployeeDetails.Your current statement won't work because it only uses table OfficeDetails, but is selecting on column Username which is in table EmployeeDetails.CODO ERGO SUM
OK thanks for the quick response, trust me to forget the Join after asking about it!So, would it be; SELECT OfficeName FROM OfficeDetails JOIN EmployeeDetails WHERE “Username.Text” = Username AND EmployeeNum.EmployeeDetails = EmployeeNum.OfficeDetails |
|
|
desperatemeasures
Starting Member
4 Posts |
Posted - 2010-03-09 : 16:27:34
|
quote: Originally posted by desperatemeasuresOK thanks for the quick response, trust me to forget the Join after asking about it!So, would it be; SELECT OfficeName FROM OfficeDetails JOIN EmployeeDetails WHERE “Username.Text” = Username AND EmployeeNum.EmployeeDetails = EmployeeNum.OfficeDetails
Or would it be; SELECT OfficeName FROM OfficeDetails JOIN EmployeeDetails ON EmployeeNum.EmployeeDetails = EmployeeNum.OfficeDetails WHERE “Username.Text” = Username |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-09 : 17:03:43
|
SELECT OfficeName FROM OfficeDetails JOIN EmployeeDetails ON EmployeeDetails.EmployeeNum = OfficeDetails.EmployeeNum WHERE “Username.Text” = EmployeeDetails.UsernameAs you can see the tablename comes first then dot then column name.But I don't know what is “Username.Text”? No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
desperatemeasures
Starting Member
4 Posts |
Posted - 2010-03-09 : 17:10:57
|
quote: Originally posted by webfred SELECT OfficeName FROM OfficeDetails JOIN EmployeeDetails ON EmployeeDetails.EmployeeNum = OfficeDetails.EmployeeNum WHERE “Username.Text” = EmployeeDetails.UsernameAs you can see the tablename comes first then dot then column name.But I don't know what is “Username.Text”? No, you're never too old to Yak'n'Roll if you're too young to die.
Ah, yes, thanks! Username.Text is the VB.NET textbox I'm getting the user input from. |
|
|
|
|
|
|
|