VB.NET Reportviewer如何使Inner Join正常工作

时间:2014-07-08 15:11:03

标签: sql vb.net reportviewer

我有2张桌子

tableAssembly,包含列SerialNumber,Brick1SerialNumber,Brick2SerialNumber,DateInserted

tableBricks,包含列SerialNumber,Lot,Weight,DateMeasured

在VB.NET(WinForms)中,我已经能够使reportviewer控件工作并打印出来自两个表的信息,并启用/禁用列,并应用LIKE等过滤器

如果不清楚tableAssembly.Brick1SerialNumber = tableBricks.SerialNumber

我现在要做的是当用户从tableAssembly打印出报告时,我希望他们能够根据Brick1SerialNumber.Lot或Brick2SerialNumber.Lot或Brick1SerialNumber.DateMeasured或Brick2SerialNumber.DateMeasured进行过滤

我明白我需要INNER JOIN tableAssembly.Brick1SerialNumber = tableBricks.SerialNumber AND tableAssembly.Brick2SerialNumber = tableBricks.SerialNumber

我是否还需要将tableBricks中的其他列INNER JOIN连接到tableAssembly中的列?或者Brick1SerialNumber的INNER JOIN = SerialNumber和Brick2SerialNumber = Serial是否可以根据.Lot进行过滤?

1 个答案:

答案 0 :(得分:0)

有两种方法可以解决这个问题。 1)连接到tableBricks两次(可能不需要这个条件)或使用两个外键附加一次(适用于此条件,但不适用于所有条件)。

尝试此查询:

SELECT tableAssembly.*
FROM tableAssembly INNER JOIN tableBricks 
   ON tableAssembly.Brick1SerialNumber = tableBricks.SerialNumber 
   OR tableAssembly.Brick2SerialNumber = tableBricks.SerialNumber
WHERE tableBricks.Lot = 99 --actually means Brick1.Lot or Brick2.Lot
  AND tableBricks.DateMeasured = '1/1/2000'

如果您需要Brick1和Brick2的特定批次或DateMeasured(不是相同的值),请尝试此查询:

SELECT tableAssembly.*
FROM tableAssembly INNER JOIN tableBricks AS tableBricks1
   ON tableAssembly.Brick1SerialNumber = tableBricks1.SerialNumber 
INNER JOIN tableBricks AS tableBricks2
   ON tableAssembly.Brick2SerialNumber = tableBricks2.SerialNumber
WHERE tableBricks1.Lot = 98
  AND tableBricks2.Lot = 99
  AND tableBricks1.DateMeasured = '1/1/2000'
  AND tableBricks2.DateMeasured = '1/2/2000'