我的查询的SQL问题

时间:2011-04-29 07:16:30

标签: sql

SELECT    
   dbo.pi_employee.emp_firstname, dbo.pi_employee.emp_lastname, 
   dbo.pi_employee.emp_no, dbo.pi_employee.emp_cnic, 
   dbo.pi_employee.emp_currentadd, dbo.pi_employee.emp_cellph, 
   dbo.pi_employee.emp_birthday, pi_jobtitle_1.jobtitle_name, 
   dbo.pi_employee.emp_joindate, dbo.pi_education.edu_degree, 
   dbo.pi_education.edu_year, dbo.pi_employee.emp_pension, 
   dbo.pi_employee.emp_age, dbo.pi_employee.emp_service, 
   dbo.pi_employee.emp_terminate, dbo.pi_employee.emp_termdate, 
   dbo.pi_employee.emp_basicofpay, dbo.pi_employee.emp_terminationreason, 
   dbo.pi_employee.emp_terminationdate, dbo.pi_employee.emp_status, 
   dbo.pi_employee.emp_gender, dbo.pi_employee.emp_maritalstatus, 
   dbo.pi_employee.emp_paymethod, dbo.pi_employee.emp_leaveentitle, 
   dbo.pi_employee.emp_confirmation, dbo.pi_employee.emp_title, 
   dbo.pi_employee.emp_basicamount, dbo.pi_salgrade.salgrade_name, 
   dbo.tbl_emp_status.StatusName, dbo.pi_skills.skill_type, 
   dbo.pi_location.loc_name, pi_location_1.loc_name AS wcity, 
   dbo.pi_jobtitlehist.jthSaleGradetype, dbo.pi_workexp.exp_serperiod, 
   dbo.pi_employee.emp_domicile, dbo.pi_skills.skill_type AS Skill, 
   dbo.pi_skills.skill_exp, dbo.pi_education.edu_degree AS Degree, 
   dbo.pi_education.edu_uni, dbo.pi_education.edu_distinction, 
   dbo.pi_lochistory.lhstart_date, dbo.pi_lochistory.lhend_date
FROM         
   dbo.pi_location 
RIGHT OUTER JOIN
   dbo.pi_workexp 
RIGHT OUTER JOIN
   dbo.pi_employee ON dbo.pi_workexp.emp_no = dbo.pi_employee.emp_no 
LEFT OUTER JOIN
   dbo.pi_jobtitlehist ON dbo.pi_employee.emp_no = dbo.pi_jobtitlehist.emp_no ON 
                  dbo.pi_location.loc_id = dbo.pi_employee.emp_location_id 
LEFT OUTER JOIN
   dbo.pi_salgrade ON dbo.pi_employee.emp_salgrade_id = dbo.pi_salgrade.salgrade_id   
LEFT OUTER JOIN
   dbo.tbl_emp_status ON dbo.pi_employee.emp_status = dbo.tbl_emp_status.StatusID 
LEFT OUTER JOIN
   dbo.pi_skills ON dbo.pi_employee.emp_no = dbo.pi_skills.emp_no 
LEFT OUTER JOIN
   dbo.pi_location AS pi_location_1 
INNER JOIN
   dbo.pi_lochistory ON pi_location_1.loc_id = dbo.pi_lochistory.loc_id ON dbo.pi_employee.emp_no = dbo.pi_lochistory.emp_no 
LEFT OUTER JOIN
   dbo.pi_education ON dbo.pi_employee.emp_no = dbo.pi_education.emp_no 
LEFT OUTER JOIN
   dbo.pi_jobtitle AS pi_jobtitle_1 ON dbo.pi_employee.emp_jobtitle_id = pi_jobtitle_1.jobtitle_id

我正在编写sql查询来实现不同的场景,但问题是它提供了重复的值。我也写了不同的顺序,但结果是一样的,任何人都可以帮我解决这个问题。


编辑 - 具有别名的表名的相同查询:

SELECT    
   em.emp_firstname, em.emp_lastname, 
   em.emp_no, em.emp_cnic, 
   em.emp_currentadd, em.emp_cellph, 
   em.emp_birthday, jt.jobtitle_name, 
   em.emp_joindate, ed.edu_degree, 
   ed.edu_year, em.emp_pension, 
   em.emp_age, em.emp_service, 
   em.emp_terminate, em.emp_termdate, 
   em.emp_basicofpay, em.emp_terminationreason, 
   em.emp_terminationdate, em.emp_status, 
   em.emp_gender, em.emp_maritalstatus, 
   em.emp_paymethod, em.emp_leaveentitle, 
   em.emp_confirmation, em.emp_title, 
   em.emp_basicamount, sg.salgrade_name, 
   es.StatusName, s.skill_type, 
   L.loc_name, L1.loc_name AS wcity, 
   jh.jthSaleGradetype, we.exp_serperiod, 
   em.emp_domicile, s.skill_type AS Skill, 
   s.skill_exp, ed.edu_degree AS Degree, 
   ed.edu_uni, ed.edu_distinction, 
   Lh.lhstart_date, Lh.lhend_date
FROM         
   dbo.pi_location AS L
RIGHT OUTER JOIN
   dbo.pi_workexp AS we
RIGHT OUTER JOIN
   dbo.pi_employee AS em ON we.emp_no = em.emp_no 
LEFT OUTER JOIN
   dbo.pi_jobtitlehist jh ON em.emp_no = jh.emp_no ON L.loc_id = em.emp_location_id 
LEFT OUTER JOIN
   dbo.pi_salgrade AS sg ON em.emp_salgrade_id = sg.salgrade_id   
LEFT OUTER JOIN
   dbo.tbl_emp_status AS es ON em.emp_status = es.StatusID 
LEFT OUTER JOIN
   dbo.pi_skills AS s ON em.emp_no = s.emp_no 
LEFT OUTER JOIN
   dbo.pi_location AS L1 
INNER JOIN
   dbo.pi_lochistory AS Lh ON L1.loc_id = Lh.loc_id ON em.emp_no = Lh.emp_no 
LEFT OUTER JOIN
   dbo.pi_education ed ON em.emp_no = ed.emp_no 
LEFT OUTER JOIN
   dbo.pi_jobtitle AS j ON em.emp_jobtitle_id = j.jobtitle_id

1 个答案:

答案 0 :(得分:0)

是的,既然你在使用那些没有on子句的连接的笛卡尔产品,这会产生很多看似“重复值”的东西,但实际上只是两个表组合集合的产物。 / p>