加入ms访问

时间:2012-10-07 05:24:23

标签: sql join ms-access-2003

我有3张桌子

  1. Payment_Schedule(Event_ID,Event_Type,Event_Incharge,Event_Date)
  2. Product_AMC(AMC_ID,Customer_ID)
  3. Item_Order(Order_ID,Customer_ID)
  4. 对于Payment_Schedule中的记录,Event_ID是AMC_ID或Order_ID。如果Event_ID是AMC_ID,那么Event_Type将是“AMC”,如果它是Order_ID,则Event_Type将是“Order”。

    现在我的问题是我不知道如何获取Customer_ID以及Payment_Schedule的所有字段。

    我正在使用MS Access 2003.请帮助。

1 个答案:

答案 0 :(得分:2)

显示所有客户及其所有payment_schedules

select PS.Event_ID, PS.Event_Type, PS.Event_Incharge, PS.Event_Date, PA.Customer_ID
from (Product_AMC PA
inner join Payment_Schedule PS on (PS.Event_Type='AMC' and PS.Event_ID=PA.AMC_ID))
union all
select PS.Event_ID, PS.Event_Type, PS.Event_Incharge, PS.Event_Date, IO.Customer_ID
from (Item_Order IO
inner join Payment_Schedule PS on (PS.Event_Type='Order' and PS.Event_ID=IO.Order_ID))
order by Customer_ID

对于单个客户,请说'ABC'

select PS.Event_ID, PS.Event_Type, PS.Event_Incharge, PS.Event_Date, PA.Customer_ID
from (Product_AMC PA
inner join Payment_Schedule PS on (PS.Event_Type='AMC' and PS.Event_ID=PA.AMC_ID))
where PA.Customer_ID = 'ABC'
union all
select PS.Event_ID, PS.Event_Type, PS.Event_Incharge, PS.Event_Date, IO.Customer_ID
from (Item_Order IO
inner join Payment_Schedule PS on (PS.Event_Type='Order' and PS.Event_ID=IO.Order_ID))
where IO.Customer_ID = 'ABC'