Results 1 to 5 of 5

Thread: [RESOLVED] MySql Join - Is this possible in Mysql?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2017
    Posts
    98

    Resolved [RESOLVED] MySql Join - Is this possible in Mysql?

    I have 2 tables:

    `contact_domain`

    contact_domain_id (INT) [Primary Key]
    display_name VARCHAR(30)



    `contact_lines`
    contact_line_id (INT) [Primary Key]
    contact_domain_id (INT) [FK]
    contact_entry_type_id (INT)
    actual_data VARCHAR(30)



    When i run the following query:

    select
    `contacts_domain`.`display_name` AS `display_name`,
    if((`contacts_lines`.`contact_entry_type_id` = 1),`contacts_lines`.`actual_data`,_latin1'') AS `Landline`,
    if((`contacts_lines`.`contact_entry_type_id` = 2),`contacts_lines`.`actual_data`,_latin1'') AS `Mobile`,
    if((`contacts_lines`.`contact_entry_type_id` = 3),`contacts_lines`.`actual_data`,_latin1'') AS `Fax`,
    if((`contacts_lines`.`contact_entry_type_id` = 4),`contacts_lines`.`actual_data`,_latin1'') AS `Email`,
    if((`contacts_lines`.`contact_entry_type_id` = 5),`contacts_lines`.`actual_data`,_latin1'') AS `Website`
    from
    (`contacts_domain` join `contacts_lines` on((`contacts_domain`.`contact_domain_id` = `contacts_lines`.`contact_domain_id`)))

    The record set returns:
    Code:
    display_name	Landline	Mobile		Fax		Email			Website
    Joe Soap 	555214544
    Joe Soap 			7814544
    Joe Soap 					4544444
    Joe Soap 							joe@somewhere
    Joe Soap 										www.somewhere.com

    What I am looking for is:
    Code:
    display_name	Landline	Mobile		Fax		Email			Website
    Joe Soap 	555214544	7814544		4544444		joe@somewhere		www.somewhere.com
    Is this possible in Mysql?

    Any help would be appreciated
    Last edited by si_the_geek; Mar 16th, 2018 at 04:17 AM. Reason: added Code tags for formatting data

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: MySql Join - Is this possible in Mysql?

    Join to the second table multiple times (with aliases), once for each type of info you want to grab, using appropriate Where clauses, eg:
    Code:
    select
    `contacts_domain`.`display_name` AS `display_name`,
    `Landline`.`actual_data` AS `Landline`,
    `Mobile`.`actual_data` AS `Mobile`,
    ...
    from
    `contacts_domain` 
    LEFT join `contacts_lines` as `Landline` on (`contacts_domain`.`contact_domain_id` = `contacts_lines`.`contact_domain_id` AND `contacts_lines`.`contact_entry_type_id` = 1)
    LEFT join `contacts_lines` as `Mobile` on (`contacts_domain`.`contact_domain_id` = `contacts_lines`.`contact_domain_id` AND `contacts_lines`.`contact_entry_type_id` = 2)
    ...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2017
    Posts
    98

    Re: MySql Join - Is this possible in Mysql?

    Thanks, I tried this:

    select
    `contacts_domain`.`display_name` AS `display_name`,
    `Landline`.`actual_data` AS `Landline`,
    `Mobile`.`actual_data` AS `Mobile`
    from
    `contacts_domain`
    LEFT join `contacts_lines` as `Landline` on (`contacts_domain`.`contact_domain_id` = `contacts_lines`.`contact_domain_id` AND `contacts_lines`.`contact_entry_type_id` = 1)
    LEFT join `contacts_lines` as `Mobile` on (`contacts_domain`.`contact_domain_id` = `contacts_lines`.`contact_domain_id` AND `contacts_lines`.`contact_entry_type_id` = 2)

    But i get the following error:

    Unknown Column 'contacts_lines.contact_domain_id' in 'On Clause'

    Even though the column exists in that table??

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: MySql Join - Is this possible in Mysql?

    Ah yes, I missed that part... in the On clauses you need to use the aliases rather than the original table name, eg:

    Code:
    LEFT join `contacts_lines` as `Landline` on (`contacts_domain`.`contact_domain_id` = `Landline`.`contact_domain_id` AND `Landline`.`contact_entry_type_id` = 1)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2017
    Posts
    98

    Re: MySql Join - Is this possible in Mysql?

    Thank you si_the_geek, works perfectly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width