假设我有一张桌子旁边的:医生和工作日:
DocNumbers | idNum
118 | 11
119 | 12
120 | 13
121 | 14
122 | 15
注意:医生可以在几个不同的工作日工作。
DocNum | Workday |AmountOfHours |
118 | 1 | 8 |
118 | 3 | 9 |
120 | 1 | 6 |
121 | 3 | 5 |
122 | 4 | 7 |
我想创建一个新表,其中包含在第1天和第3天工作的所有医生的id—这意味着我将得到一个只包含118个的表。
到目前为止我有:
SELECT distinct Doctors.doctorNumber, idNum
FROM Doctors, Workdays
WHERE Workdays.dayInWeek in (1,3)
AND Workdays.doctorNumber=Doctors.doctorNumber
但似乎我得到了120和121这样的无关结果。
所以“IN”更像“OR”。似乎找不到“and”的等价物?
最佳答案
如果您加入Workdays表两次,每天检查一次,这很容易做到:
select Doctors.DocNumbers, Doctors.idNum
from Doctors
inner join Workdays as Workdays1 on Workdays1.DocNum = Doctors.DocNumbers and Workdays1.Workday = 1
inner join Workdays as Workdays3 on Workdays3.DocNum = Doctors.DocNumbers and Workdays3.Workday = 3;
http://www.sqlfiddle.com/#!2/4c530/3
关于mysql - 同时选择两个条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20292886/