A
A
Alex2019-01-15 11:23:21
SQL
Alex, 2019-01-15 11:23:21

Problems with fetching fields from the database?

5c3d964f2e894419602414.jpeg
For the depicted database schema, you need to create a query that will return a list of all accounts. For each account, select the fields Account Number, Account Date, Customer Name, and Customer Name referred to by the current account customer.
Let's say a query on the first 3 fields, I would do like this:

SELECT Invoices.Id, Customers.CBilling_date, Customers.Name
  FROM Invoices JOIN Customers ON (Customers.Id = Invoices.Customer_ID) 
  GROUP BY Invoices.Billing_date

But I don’t understand how to select the name of the client referred to by the client of the current account. After all, there is no such field at all in any table.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idShura, 2019-01-15
Gutter

SELECT I.Id, 
       C.CBilling_date, 
       C.Name, 
       C2.Name -- Имя клиента, на которого ссылается клиент текущего счета
  FROM Invoices I 
       left join Customers C ON C.Id = I.Customer_ID
       left join Customers C2 ON C2.Id = C.Referred_ID

GROUP BY I.Billing_date

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question