好的,我又遇到了麻烦。
我的ERD中有查询问题的部分是我的超类,超类如下:
电源(超) 非外科学(分) 手术(子) 药物(分)
主键是'OrderNumber'
在病房工作的工作人员提出项目申请,我想查看所有要求的项目,以及收到并订购所有非药物项目的日期(来自子类别)外科和外科手术)
到目前为止,我有这个:
SELECT requisition.ReqNumber, staff.staffnumber, ward.wardname, itemname
FROM requisition, staff, supply, ward, ward_staff, req_supply, nonsurgical, surgical
WHERE requisition.staffnumber = staff.staffnumber
AND requisition.reqnumber = req_supply.reqnumber
AND supply.ordernumber = req_supply.ordernumber
AND staff.staffnumber = ward_staff.staffnumber
AND ward_staff.wardnumber = ward.wardnumber
AND supply.ordernumber = nonsurgical.ordernumber
OR supply.ordernumber = surgical.ordernumber;
所以基本上如果supercalss的主键与手术中的主键相同......或者与非手术中的相同,请告诉我那个请求!问题在于子课程,但我迷失了
感谢您提供任何帮助
答案 0 :(得分:3)
在OR条件周围加上括号以获得正确的顺序。
...
AND (supply.ordernumber = nonsurgical.ordernumber
OR supply.ordernumber = surgical.ordernumber);
答案 1 :(得分:3)
OR运算符的优先级低于AND运算符,因此您需要对最后两个子句使用括号,否则仅满足最后一个OR子句就足够了。
所以:
SELECT requisition.ReqNumber, staff.staffnumber, ward.wardname, itemname
FROM requisition, staff, supply, ward, ward_staff, req_supply, nonsurgical, surgical
WHERE requisition.staffnumber = staff.staffnumber
AND requisition.reqnumber = req_supply.reqnumber
AND supply.ordernumber = req_supply.ordernumber
AND staff.staffnumber = ward_staff.staffnumber
AND ward_staff.wardnumber = ward.wardnumber
AND (supply.ordernumber = nonsurgical.ordernumber
OR supply.ordernumber = surgical.ordernumber);
答案 2 :(得分:1)
我遇到了几个AND子句和最终OR子句的类似问题,请尝试围绕括号中的两个ORed子句。
SELECT requisition.ReqNumber, staff.staffnumber, ward.wardname, itemname
FROM requisition, staff, supply, ward, ward_staff, req_supply, nonsurgical, surgical
WHERE requisition.staffnumber = staff.staffnumber
AND requisition.reqnumber = req_supply.reqnumber
AND supply.ordernumber = req_supply.ordernumber
AND staff.staffnumber = ward_staff.staffnumber
AND ward_staff.wardnumber = ward.wardnumber
AND
(supply.ordernumber = nonsurgical.ordernumber
OR supply.ordernumber = surgical.ordernumber);
答案 3 :(得分:1)
您的主要问题是您使用难以保持直接连接语法。因此,您实际上正在使用左连接。
SELECT r.ReqNumber,
St.staffnumber,
w.wardname, itemname
FROM requisition r
Inner join staff st
On r.staffnumber = st.staffnumber
Inner join req_supply rs
On r.reqnumber = rs.reqnumber
Inner join supply su
On su.ordernumber = rs.ordernumber
Inner join ward_staff wst
On wst.staffnumber = st.staffnumber
Inner join ward w
On w.wardnumber = wst.wardnumber
Inner join nonsurgical nsr
On su.ordernumber = nsr.ordernumber
Left join surgical sr
On su.ordernumber = sr.ordernumber ;