EasyE
EasyE

Reputation: 580

Trying to use Join to get data from two table into one query search using Join

I am issuing an SQL query in my visual studio application to search a database. I am having trouble joining two tables into one query search. I am looking to combine the First and Last name of an employee and match it up to a phone number. In one table I have an abbreviated name, and on the other table I have the full name.

Example
enter image description here

enter image description here

Code

cmdTxt.Append("SELECT partner.staffid, staff.Forename,staff.surname FROM tblpartner LEFT JOIN tblstaff ON staff.StaffID = partner.staffid ORDER BY staff.forename , staff.surname ");

I would Like to have a field that shows the Forename , surename, and phone number in one query search

Upvotes: 0

Views: 886

Answers (1)

GendoIkari
GendoIkari

Reputation: 11914

You are using "partner" and "staff" as table aliases, but you aren't assigning those aliases to the table names. If you want to use those instead of the full table names, you need:

SELECT partner.staffid, staff.Forename,staff.surname, partner.phone
FROM tblpartner partner
LEFT JOIN tblstaff staff ON staff.StaffID = partner.staffid
ORDER BY staff.forename , staff.surname

As general advice, get your query working outside of your string literal; enter it into SQL Server Management Studio or something similar. Had you done that, it should have clearly showed you what the problem was. Then when you have your query set how you want, you can copy it into your Command Text in your Visual Studio application.

Upvotes: 3

Related Questions