我有各种要从中提取的表/视图:Invoice
、Detail
、contact
、party
和。address
remarks
基本上我需要从remarks
表中提取数据,将其与party
表关联的唯一方法是访问表的其余部分remarks
以引用其id
列,该列是表上的外键party
。
这id
也是Invoice
视图中的一列,因此当FROM
子句执行时,它结合了Invoice
侧面的 id 字段,并且没有任何东西与其他remarks
列相关。此外,不能更改任一表列上的名称:
SELECT *
FROM Invoice as invoice_view LEFT OUTER JOIN
Detail as details ON invoice_view.transact = details.transact LEFT OUTER JOIN
contact AS co_contact ON invoice_view.company = co_contact.party LEFT OUTER JOIN
contact AS cp_contact ON invoice_view.company = cp_contact.party INNER JOIN
party as main_party ON invoice_view.party = main_party.party INNER JOIN
party as pay_party ON invoice_view.pay = pay_party.party INNER JOIN
party as rec_party ON invoice_view.rec = rec_party.party LEFT OUTER JOIN
contact as rec_contact ON rec_party.party = rec_contact.party INNER JOIN
address as rec_address ON rec_contact.party = rec_address.party AND rec_contact.address = rec_address.addresscode LEFT OUTER JOIN
contact AS pay_contact ON pay_party.party = pay_contact.party INNER JOIN
adddress AS pay_address ON pay_contact.party = pay_address.party AND pay_contact.address = pay_address.addresscode AND pay_party.party = pay_address.party LEFT OUTER JOIN
--What I tried to get it working
remark as cp_remark CROSS JOIN
party as custom_party
WHERE
( (custom_party.party = pay_party.party) AND (custom_party.id = cp_remark.id) OR (cp_remark.id IS NULL) OR (custom_party.remark = 0))
--More where statements that have no affect on this