如何对同一字段的不同值进行分组

时间:2014-12-01 23:11:10

标签: mysql

我写过

 SELECT DISTINCT r.id_destination
               , lo.location1
               , lo.location2
               , lo.destination
               , lo.zip
               , ld.id_field
               , da.identification entity
               , da.data
            FROM pl_records r
            JOIN pl_records_detail rd 
              ON r.id_order = rd.id_order 
       LEFT JOIN pl_attribue at 
              ON rd.product_attribute_id = at.id_product_attribute 
       LEFT JOIN pl_profile pr 
              ON at.id_attribute = pr.id_attribute 
            JOIN pl_location lo 
              ON r.id_address_delivery = lo.id_address 
            JOIN pl_situation si 
              ON r.current_state = si.id_order_state 
       LEFT JOIN pl_location_detail  
              ON r.id_cart = ld.id_cart 
       LEFT JOIN pl_data da 
              ON ld.id_field = da.id_field
           WHERE r.id_record = 6

它返回由于字段“entity”和“data”而重复的输出,因为它们包含许多值!

id_destination location1         location2 destination zip id_field entity data
            55 east coast street ON        CN          454       28     89 Please process it with safe standards      
            55 east coast street ON        CN          454       28     90 Payment will be COD
            55 east coast street ON        CN          454       28     78 updates not necessary
            55 east coast street ON        CN          454       28     92 no withdrawal of details
            55 east coast street ON        CN          454       28     99 added records

如何防止重复值?或者如何对最后两个字段的不同值进行分组

1 个答案:

答案 0 :(得分:0)

也许您正在寻找group_concat()

SELECT r.id_destination, lo.location1, lo.location2, lo.destination, lo.zip,
       GROUP_CONCAT(DISTINCT ld.id_field) as id_fields,
       GROUP_CONCAT(DISTINCT da.identification) AS entities
da.data
FROM pl_records r JOIN
     pl_records_detail rd
     ON r.id_order = rd.id_order LEFT JOIN 
     pl_attribue at
     ON rd.product_attribute_id = at.id_product_attribute LEFT JOIN
     pl_profile p
     ON at.id_attribute = pr.id_attribute JOIN
     pl_location lo
     ON r.id_address_delivery = lo.id_address JOIN
     pl_situation si
     ON r.current_state = si.id_order_state LEFT JOIN
     pl_location_detail
     ON r.id_cart = ld.id_cart LEFT JOIN
     pl_data da
     on ld.id_field = da.id_field
WHERE r.id_record = 6
GROUP BY r.id_destination, lo.location1, lo.location2, lo.destination, lo.zip