为什么我的SELECT只返回行的子集?

时间:2014-09-18 09:44:29

标签: sql oracle

我是第一次使用Oracle数据库,又偶然发现了另一个问题。当我想用一些JOINS选择表中的所有行时,我只得到大约15.000行的前350行。

任何人都知道我不知道某个地方是否存在设定限制?

如果需要,以下是我的查询:

SELECT orders.plant, orders.workcenter, workcenters.occupied,
       workcenters.section, workcentersections.section, orders.capacitycat,
       orders.week, orders.earlieststartdate, orders.lateststartdate,
       orders.useropstatus, orders.programstatus, orders.reqhours,
       orders.finishdate, orders.reqquantity, orders.material, parts.TYPE,
       parttypes.TYPE, orders.ordernumber, orders.operation,
       orders.preoperation, orders.seqoperation, orders.projectcode,
       orders.queuetime, orders.hoursworked, orders.operationtext,
       orders.shorttext
  FROM (((orders INNER JOIN workcenters ON orders.workcenter =
                                                              workcenters.code)
       INNER JOIN
       workcentersections ON workcenters.section = workcentersections.ID)
       INNER JOIN
       parts ON orders.material = parts.material)
       INNER JOIN
       parttypes ON parts.TYPE = parttypes.ID

1 个答案:

答案 0 :(得分:1)

假设您的ORDERS表包含15000行且原始查询仅返回350行,则可以使用OUTER JOIN替换(INNER)JOIN:

SELECT orders.plant, orders.workcenter, workcenters.occupied,
       workcenters.section, workcentersections.section, orders.capacitycat,
       orders.week, orders.earlieststartdate, orders.lateststartdate,
       orders.useropstatus, orders.programstatus, orders.reqhours,
       orders.finishdate, orders.reqquantity, orders.material, parts.TYPE,
       parttypes.TYPE, orders.ordernumber, orders.operation,
       orders.preoperation, orders.seqoperation, orders.projectcode,
       orders.queuetime, orders.hoursworked, orders.operationtext,
       orders.shorttext
  FROM orders
       LEFT OUTER JOIN workcenters ON orders.workcenter = workcenters.code
       LEFT OUTER JOIN workcentersections 
            ON workcenters.section = workcentersections.ID
       LEFT OUTER JOIN parts ON orders.material = parts.material
       LEFT OUTER JOIN parttypes ON parts.TYPE = parttypes.ID

这将为您提供ORDERS的所有行(如果您没有严格的1:N关系,则可能会出现重复行。)

然后,你应该用INNER JOIN逐个替换LEFT OUTER JOIN并检查每个修改过的查询的行数,找出哪些JOIN负责丢失的数据。