SQL statement to select a customer record and every phone number associated with it

  Kiến thức lập trình

I have a Customers table and a CustomerPhones tables. The Customers table has ID, FirstName, Lastname. The Phone Numbers table has CustomerID, PhoneNumber, PhoneType. Lets say I have the following data in the Customers table
ID Firstname Lastname
1 Bob Smith
and then in the phone numbers table
customerid phonenumber phonetype
1 111111111 H
1 222222222 C
1 333333333 C

If I use the simple join sql, I will get 3 tows.
Select c.id, c.FirstName, c.LastName, P.PhoneNumber, P.Phonetype from customers c
Join CustomerPhoneNumbers P
on p.customerid = c.id
Results
ID Firstname Lastname phonenumber phonetype
1 Bob Smith 111111111 H
1 Bob Smith 222222222 C
1 Bob Smith 333333333 C
How do I turn that into results with 1 row but with each number?

ID Firstname Lastname phonenumber phonetype phonenumber phonetype phonenumber phonetype
1 Bob Smith 111111111 H 222222222 C 333333333 C
and could it be dynamic since customers can have any amount of phone numbers? Thank you for the time and help.

New contributor

Jason Solida is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT