How to group field data?

前端 未结 2 1535

I have sql query to show data

\"SELECT
  `artikel`.`foto_naam`,
  `fotografer`.`id`,
  `fotografer`.`name_fotografer`,
  `customer`.`first_name`,
  `customer`.`l         


        
相关标签:
2条回答
  • 2021-01-24 22:47

    One thing you can do in mysql is to wrap you query with a group by and group_concat(). Something like this:

    SELECT
        name, 
        email, 
        group_concat(customer) AS customers, 
        group_concat(invoice_id) AS invoice_ids
    FROM
        (...query you already have..)
    GROUP BY
        name, email
    

    Result will look like something like this:

    name    email               customers           invoice_ids
    
    heko    heko@gmail.com      pae,edwin           15,16
    Lekto   lekto@gmail.com     edwin,risa,edwin    11,12,13
    

    PLZ SEE FIDDLE

    0 讨论(0)
  • 2021-01-24 23:03

    Odd question, but if you REALLY NEED THIS in MySQL then you could use USER VARIABLES to set the Name and email column to '' (empty string) for subitems in the list.

    DEMO FIDDLE or e.g.

    select 
        if(@tName!= Name, if(@'tNAME':= Name, Name,Name) , '' ),
        if(@tEmail!= Email, if(@'tEmail':= Email, Email ,Email) , '' ),
        customer,
        invoice_id,
        /* blah blah blah */
    from 
        tables
        join (select @'tName':='', @'tEmail':='') T on 1=1 
    /* blah blah blah */
    

    I provide the join (select @'tName':='') T on 1=1 to re-initialize the user variable just in case you can't control them.

    0 讨论(0)
提交回复
热议问题