Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2004
    Posts
    1

    Question Unanswered: MySQL => combining 2 rows

    Is it possible in MySQL to combine 2 rows into 1?
    Example:
    Code:
    Table1:    
    Id Name   
    1   A
    2   B
    3   C
    
    Table2:    
    Tab1_Id  Tab2_Id   
    1            3
    1            2
    2            2
    3            1
    
    Table3:    
    Id Name   
    1   D
    2   E
    3   F
    
    What I want to achieve is to see the following when my SQL Statement is executed:
    Tab1_Id  Tab3_name
    1            F,E
    2            E
    3            D
    is this possible with any SELECT statement?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    possible in one query, yes:
    Code:
    select Table1.id    as Tab1_Id
         , Table3.Name  as Tab3_name
      from Table1
    inner
      join Table2
        on Table1.id 
         = Table2.Tab1_Id
    inner
      join Table3
        on Table2.Tab3_Id  -- obvious typo
         = Table3.id 
    order 
        by 1,2
    however, in order to collapse or "denormalize" E and F onto one output row, you will need either the GROUP_CONCAT function in version 4.1, or do it in your scripting language
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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