将两行合并为一个添加唯一数据

时间:2016-07-12 16:49:22

标签: mysql mysqli phpmyadmin

对于MYSQL数据库中的每个订单,我有两行,它们除了包含所有相同的信息外:

拾取位置-ID

我想知道是否有办法在连接

时将行合并为一行 EX row1的

pickup-location-id

EX row2的

pickup-location-id2

进入 ONE ROW ,并使用订单ID。

这是我最接近的......

SELECT *
    FROM `t_order-transportation`
    INNER JOIN (SELECT `pickup-location` AS `pickup-location-2`,
                `order-id`
               FROM `t_order-transportation`
               GROUP  BY `pickup-location`
               HAVING COUNT(id) > 1) dup
           ON `t_order-transportation`.`order-id` = dup.`order-id`

这几乎是我想要的,但它似乎显示4行,2为原件,2为新行,并且两种方式都是分段。

Sample of select return || RED IS ORIGINAL ROWS

MYSQL FIDDLE:http://sqlfiddle.com/#!9/4b441/1

1 个答案:

答案 0 :(得分:0)

我建议一个简单的内连接

SELECT a.*, b.*
FROM `t_order-transportation` as a
INNER JOIN `t_order-transportation` as b 
              on ( a.`order-id` = b.`order-id` 
                 and a.`pickup-datetime` < b.`pickup-datetime`)

您可以

的位置
SELECT a.*, b.`pickup-location`
FROM `t_order-transportation` as a
INNER JOIN `t_order-transportation` as b 
              on (a.`order-id` = b.`order-id`
               and a.`pickup-datetime` < b.`pickup-datetime`)