Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
deniseaddy
Starting Member
12 Posts |
Posted - 2009-04-14 : 14:09:31
|
| Hi,I have four tables:payments_customerspayments_affiliatescustomersaffiliatespayments_customers has a foreign key linking to table customers idpayments_affiliates has a foreign key linking to table affiliates idBoth payments tables have the same schema: id, company, value, date and the only difference is the foreign key linking the payment record to either the affiliate or customer table.In a single SQL query (depending on parameters passed to sproc) how can I...1. Select all the data from both tables2. Or select only data from payments_customer or payments_affiliate3. Or select only the data where the affiliate/customer id matches the payment_customer/payment_affiliate recordThanks. |
|
|
Kokkula
Starting Member
41 Posts |
Posted - 2009-04-15 : 03:01:16
|
| Hello,Try thesepayments_customers and customers tables can be linked in the waySELECT Payments.ID,Payments.Company,Payments.Value,Payments.DateFROM payments_Customers AS PaymentsINNER JOIN Customers AS CustON Cust.ID = Payments.IDWHERE Cust.ID = @CustID -- Parameter you wantpayments_affiliates and affiliates tables can be linked in the waySELECT Payments.ID,Payments.Company,Payments.Value,Payments.DateFROM payments_Customers AS PaymentsINNER JOIN affiliates AS AffON Aff.ID = Payments.IDWHERE Aff.ID = @AffID -- Parameter you wantIf you want all the data from the two tables then SELECT Payments.ID,Payments.Company,Payments.Value,Payments.DateFROM payments_Customers AS PaymentsINNER JOIN Customers AS CustON Cust.ID = Payments.IDWHERE Cust.ID = @CustID -- Parameter you wantUNIONSELECT Payments.ID,Payments.Company,Payments.Value,Payments.DateFROM payments_Customers AS PaymentsINNER JOIN affiliates AS AffON Aff.ID = Payments.IDWHERE Aff.ID = @AffID -- Parameter you wantHope its clear and helpful...Thanks,Pavan |
 |
|
|
deniseaddy
Starting Member
12 Posts |
Posted - 2009-04-15 : 06:06:56
|
| Thanks for your help! |
 |
|
|
|
|
|
|
|