Reputation: 334
I have 4 tables (as shown in the picture), I want to select Invoice, Client for which invoice is issued, And all the items for which the invoice is issued.
I cannot figure out how? How should I apply joins, or should i use sub queries?
Please helpe me, I am really struck.
Thanks in Advance.
Upvotes: 1
Views: 81
Reputation: 3892
Select c.ClientID, i.Invoiceid, it.itemId
from Clients c
Inner Join Invoice i ON i.ClientId = c.ClientId
Inner Join InvoiceItems ii on ii.InvoiceId = i.InvoiceId
Inner Join Items it on ii.ItemId = it.ItemI
Order by c.Clientid, i.Invoiceid, it.itemId
One can add additional columns as needed.
Upvotes: 2
Reputation: 93704
Simple INNER JOIN
should work
SELECT I.ItemID,
C.ClientID,
IV.InvoiceID
FROM Items I
INNER JOIN InvoiceItems II
ON I.ItemID = II.ItemID
INNER JOIN Invoice IV
ON IV.InvoiceID = II.InvoiceID
INNER JOIN Client C
ON C.ClientID = IV.ClientID
Upvotes: 2
Reputation: 35780
What's the problem? It's simple join
Select * From InvoiceItems ii
Join Items i on ii.ItemID = i.ItemID
Join Invoice inv on ii.InvoceID = inv.InvoiceID
Join Clients c on inv.ClientID = c.ClientID
Upvotes: 1