to me I would think that this should be spread over a couple of tables, however it all depends on how your database is set up I will do it the way I would do it, and also I will do it as if they were all in 1 table
Code:
SELECT c.company_name, che.title, che.work_telephone, e.fname, e.surname, e.address, e.home_telephone FROM Company_has_employees AS che LEFT JOIN employees AS e ON che.employee_id = e.employee_id LEFT JOIN company AS c ON che.company_id = c.company_id WHERE c.company_name = 'CC' AND che.title = 'Sales Manager'"
now let me brake that down.
SELECT c.company_name, che.title, che.work_telephone, e.fname, e.surname, e.address, e.home_telephone
grabs the fields that will be displayed to the user from the appropriate tables
FROM Company_has_employees AS che LEFT JOIN Employees AS e ON che.employee_id = e.employee_id LEFT JOIN Company AS c ON che.company_id = c.company_id
uses the reference table Company_has_employees to bridge between the company info and the employee info... this is best for optimization because plugging in a unique id is cuts down on maintanence and provides the ability for 1 person to work for multiple companies / have multiple titles. The joins are based upon the reference table lining them up / pulling them in according to the appropriate unique identity.
NOTE: Table AS abbreviation is a method of shortening what you need to write... if you did not want to use the abbreviation / do not feel comfortable in how to use it I would suggest that you write out the whole table like
Code:
SELECT Company.company_name, Company_has_employees.title, Company_has_employees.work_telephone, Employees.fname, Employees.surname etc...
WHERE c.company_name = 'CC' AND che.title = 'Sales Manager'"
this is how the field is widdled down to get a specific result... in your case you want the company CC and the title of Sales Manager.
this example is assuming the database is structured somewhat like
TABLE Company_has_employees
company_id
employee_id
title
work_telephone
TABLE Company
company_id
company_name
TABLE Employees
employee_id
fname
surname
address
home_telephone
okay that was how I would do it in an optimized database, if all the info is in 1 table the query will be alot smaller but accomplish the same thing
Code:
SELECT company_name, title, work_telephone fname, surname, home_telephone, address FROM People WHERE company_name = 'CC' AND title = 'Sales Manager'
with a database structure like
TABLE People
company_name
fname
surname
title
work_telephone
address
home_telephone
both of them accomplish the same thing but in the first example the database is optimized for redundancy.
if you give us a better picture of the structure of your database we can help you create a more customized query, however I, we, suggest that you try it out yourself first and bring any questions / errors that you receive.
Bookmarks