我有 2 个表,其中包含需要加入的大量数据。问题是这 2 个表包含大部分相同的数据,连接有时会产生不希望的结果,但并不意外。结果。这是一个例子:
week_end_date nugly payroll_code rate hours check_number
--------------------------------------------------------------
2010-01-17 AU9T8K HRLY-W 13.00000 40.00000 530957
2010-01-17 AU9T8K HRLY-W 13.00000 40.00000 DD00000105382
week_end_date nugly trx_number pay_code hours rate
2010-01-17 AU9T8K ETS00000010771815 HRLY-W 40.00000 13.00000
2010-01-17 AU9T8K ETS00000010771684 HRLY-W 40.00000 13.00000
我正在查看在连接中组合的检查 # 和 trx_number,但我最终得到了一个交叉连接,因为我加入的一切都是一样的。对于我遇到的每种情况,我真的不在乎 trx_number 以哪个检查 # 结尾。
有什么想法吗?
以下是目前的结果:
week_end_date nugly payroll_code rate hours check_number trx_number
2010-01-17 AU9T8K HRLY-W 13.00000 40.00000 DD00000105382 ETS00000010771815
2010-01-17 AU9T8K HRLY-W 13.00000 40.00000 530957 ETS00000010771815
2010-01-17 AU9T8K HRLY-W 13.00000 40.00000 DD00000105382 ETS00000010771684
2010-01-17 AU9T8K HRLY-W 13.00000 40.00000 530957 ETS00000010771684
我想要的是:
week_end_date nugly payroll_code rate hours check_number trx_number
2010-01-17 AU9T8K HRLY-W 13.00000 40.00000 DD00000105382 ETS00000010771815
2010-01-17 AU9T8K HRLY-W 13.00000 40.00000 530957 ETS00000010771684
我并不关心哪个 trx_number 与哪个 check_number 对应。
这是我当前的查询:
SELECT c.week_end_date, c.nugly, c.payroll_code, c.rate, c.hours, c.check_number, t.trx_number
FROM checksByNuglyPaycode c
LEFT OUTER JOIN trxNumByNuglyPaycode t ON c.db_id = t.db_id AND c.fridate = t.fridate
AND c.nugly = t.nugly AND c.trx_type = t.trx_type AND c.payroll_code = t.pay_code
AND c.hours = t.hours AND c.rate = t.rate AND c.week_end_date = t.week_end_date
WHERE t.db_id = 'lal' AND c.nugly = 'AU9T8K' AND c.payroll_code = 'HRLY-W'
AND c.fridate = '2010-01-22' AND c.week_end_date = '2010-01-17'
ORDER BY c.fridate, c.nugly, payroll_code
where 子句显然是专门针对这种情况的,在最后的查询中,不会有 where 子句。