Skip to content
Advertisement

Adding customers name alongside customer name

based on the DB schema I am trying to get for each customer the Id, date, customer name and the customer. Can anyone help?

I am struggling with adding the name of the customer. This is what I have tried so far:

SELECT customer,name,date
from table

Advertisement

Answer

You need two joins to the customers table. That said, you really need to learn to use meaningful table aliases rather than arbitrary letters. Table aliases are your friend, and arbitrary letters aren’t so friendly.

So:

SELECT i.Id, i.BillingDate, c.Name as CustomerName,
       cr.Name as ReferredByName
FROM Invoices i LEFT JOIN
     Customers c 
     ON i.CustomerId = c.Id LEFT JOIN
     Customers cr
     ON i.ReferredBy = cr.Id;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement