2013-02-01 37 views
-1

設置:MySQL的Select語句不能讓我的頭周圍

(數量1)Employee表(僱員,employeeName)

(數量1)PayType表(payTypeID,payTypeName)

(數量1)其中,員工被分配多個收費類型PaySchedule表(payScheduleID,payScheduleName)

(數量1)結表(Employee_has_PayType)(EMPLOYEEID,payTypeID)

(數量1),員工被分配多條支付時間表(僱員,payScheduleID)結表(Employee_has_PaySchedule)

我需要什麼:

我想,分配所有EmployeeIDs一個或全部這些payTypeIDs(29,31,32)(從Employee_has_PayType),其具有payScheduleID 1(從Employee_has_PaySchedule) EmployeeIDs

的我想只有那些滿足這些標準EmployeeIDs。

到目前爲止,我已經試過這樣:

SELECT 
    Employee_has_PayType.Employee_employeeID as 'type', 
    Employee_has_PaySchedule.Employee_employeeID as 'sched' 
FROM 
    Employee_has_PayType, Employee_has_PaySchedule 
WHERE 
    Employee_has_PayType.PayType_payTypeID in (29,31,32) 
AND 
    Employee_has_PaySchedule.PaySchedule_payScheduleID = 1 
GROUP BY 
    Employee_has_PayrollSchedule.Employee_EmployeeID 

但它只返回一個有paySchedule 1.

任何幫助將不勝感激所有employeeIDs。

+1

我認爲你需要做一個JOIN表中得到你想要的結果。 –

+0

SELECT t.Employee_employeeID FROM Employee_has_PayType噸JOIN Employee_has_PaySchedule S ON t.Employee_employeeID = s.Employee_employeeID WHERE中(29,31,32)t.PayType_payTypeID AND s.PaySchedule_payScheduleID = 1 –

回答

3

嘗試:

SELECT DISTINCT 
    Employee_has_PayType.Employee_employeeID 
FROM Employee_has_PayType 
JOIN Employee_has_PaySchedule 
    ON Employee_has_PayType.Employee_employeeID = 
    Employee_has_PaySchedule.Employee_employeeID 
WHERE 
    Employee_has_PayType.PayType_payTypeID in (29,31,32) 
AND Employee_has_PaySchedule.PaySchedule_payScheduleID = 1 
+0

就是這樣。我在加入時離開了。謝謝,馬克! – Jordan