0
 i have three tables file, expense and payment. I want fetch data from 3 table based on foreign key.

我有以下三个表

文件表

FileId
FileNo

付款表

  Id
  Amount
  FK_FileID

费用表

  Id
  Amount
  FK_FileID

我想要像这样的输出

FileNo      Amount  IsPayment
10001         220         True
10001         120         False
10001         150         True

IsPayment 是逻辑字段,如果金额来自付款表 IsPayment 应该为真,如果它来自费用表,则应该为假。

4

1 回答 1

0

我想这个查询应该可以解决问题:

SELECT ft.FileNo, pt.Amount, 'True' IsPayment
FROM FileTable ft
JOIN PaymentTable pt ON pt.FK_FileID = ft.FileId
UNION ALL
SELECT ft.FileNo, et.Amount, 'False' IsPayment
FROM FileTable ft
JOIN ExpenseTable et ON et.FK_FileID = ft.FileId
于 2012-08-12T17:44:41.630 回答