Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:51:22+00:00 2026-06-10T02:51:22+00:00

I need to combine 3 phone numbers for a user into one row. The

  • 0

I need to combine 3 phone numbers for a user into one row. The end result would look like this

contactId | phone | mobile | fax

my tables look like this with the phone numbers broken into their own rows by phoneTypeId


contact_phone_link
contactId | contactPhoneNumberId
3344 | 1


contact_phone_numbers
id | phoneTypeId | phoneNumber
123 | 1 | 555-555-5555


phone_types
id | phoneTypeName
1 | Phone
2 | Mobile

3 | Fax


Dana thanks for your help, I’m getting closer to understanding this. I see how it’s grouping and naming and then grouping again.

I’ve gone through and adjusted the syntax, but I’m still having problems where it gets to ‘phone_link.phoneTypeId’. I think it’s because ‘contact_phone_link’ doesn’t hold ‘phoneTypeId’. Also it gets hung up when the SELECT section and JOIN section had the same variable name like ‘phone’ so I added a 1.

Tell me if I’m way off.

SELECT
    `ct`.`id` AS contact_id,
    `phone1`.`lineNumber` AS phone,
    `fax1`.`lineNumber` AS fax,
    `mobile1`.`lineNumber` AS mobile

FROM `cmd`.`contacts` AS ct

LEFT JOIN `cmd`.`contact_phone_link` AS `phone_link` ON (`phone_link`.`contactId` = `ct`.`id` AND `phone_link`.`phoneTypeId` = 1)
LEFT JOIN `cmd`.`contact_phone_numbers` AS `phone1` ON (`phone`.`id` = `phone_link`.`contactPhoneNumberId`)

LEFT JOIN `cmd`.`contact_phone_link` AS `fax_link` ON (`fax_link`.`contactId` = `ct`.`id` AND `fax_link`.`phoneTypeId` = 2)
LEFT JOIN `cmd`.`contact_phone_numbers` AS `fax1` ON (`fax`.`id` = `fax_link`.`contactPhoneNumberId`)

LEFT JOIN `cmd`.`contact_phone_link` AS `mobile_link` ON (`mobile_link`.`contactId` = `ct`.`id` AND `mobile_link`.`phoneTypeId` = 3)
LEFT JOIN `cmd`.`contact_phone_numbers` AS `mobile1` ON (`mobile`.`id` = `mobile_link`.`contactPhoneNumberId`)

WHERE (`ct`.`id` = 4160);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:51:23+00:00Added an answer on June 10, 2026 at 2:51 am

    Edited solution below.

    SELECT
        `ct`.`id` AS contact_id,
        `phone_sub_query`.`lineNumber` AS phone,
        `fax_sub_query`.`lineNumber` AS fax,
        `mobile_sub_query`.`lineNumber` AS mobile
    
    FROM `cmd`.`contacts` AS ct
    
    left join
        (select `phone_link`.`contactId`, `phone_number`.`lineNumber`
        from `cmd`.`contact_phone_link` AS `phone_link`
        join `cmd`.`contact_phone_numbers` AS `phone_number` ON (`phone_number`.`id` = `phone_link`.`contactPhoneNumberId`)
        where `phone_number`.`phoneTypeId` = 1) as `phone_sub_query` on `phone_sub_query`.`contactId` = `ct`.`id`
    
    left join
        (select `phone_link`.`contactId`, `phone_number`.`lineNumber`
        from `cmd`.`contact_phone_link` AS `phone_link`
        join `cmd`.`contact_phone_numbers` AS `phone_number` ON (`phone_number`.`id` = `phone_link`.`contactPhoneNumberId`)
        where `phone_number`.`phoneTypeId` = 2) as `fax_sub_query` on `fax_sub_query`.`contactId` = `ct`.`id`
    
    left join
        (select `phone_link`.`contactId`, `phone_number`.`lineNumber`
        from `cmd`.`contact_phone_link` AS `phone_link`
        join `cmd`.`contact_phone_numbers` AS `phone_number` ON (`phone_number`.`id` = `phone_link`.`contactPhoneNumberId`)
        where `phone_number`.`phoneTypeId` = 3) as `mobile_sub_query` on `mobile_sub_query`.`contactId` = `ct`.`id`
    
    WHERE (`ct`.`id` = 4160);
    

    Given your table structure, I think using sub-queries might be required. My solution actually looks a little more like @Eugene’s now, but you want to left join against an inner join. My sub-queries check the phone type and emit (a) a contact id and (b) a line number.

    Let me know if this works. I think it should be closer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to combine rows from two Excel worksheets into one long row, based
Using C++ I need to combine two distinct IDs into one 16bit integer. Then
I need to create and combine several expressions for child entity into one to
I need to combine two URL's that both contain .Path information. I would like
I need to combine a $variable with a underscore and word like this: $agent
I need to combine a folder full of pdfs into one file. However they
I have two .htaccess files that I need to combine into one. The first
I have 3 byte arrays in C# that I need to combine into one.
I need to combine one Header Table and six Detail Tables into a single
I need to combine 2 images into one. Basically all i need to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.