0

我有 2 个表order_managementorder_management2它们几乎相同...... order_idinorder_management自动递增的,而order_idinorder_management2不是。我想基于 . 合并两个表中每个字段的数据order_id

认为,

order_management 表

order_id      customer_name     fone no.      
--------      -------------    ---------
1              A                89000
2              B                78989
3              C                56464

order_management2 表

  order_id        customer_name      fone no.
 ----------      ---------------    ----------
   2                D                 89076
   3                E                 67598
   4                F                 67545

如果order_id两个表都是2然后,它应该显示如下:

 order_id        customer_name      fone no.
 ----------      ---------------    ----------
   2                B,D               78989,89076
   3                C,E               56464,67598

这个怎么做?

4

3 回答 3

1

这个对我有用:

SELECT
  t1.order_id,
  CONCAT(t1.customer_name, ',', t2.customer_name) AS customer_name,
  CONCAT(t1.phone, ',', t2.phone) AS phone_no
FROM order_management t1
JOIN order_management2 t2 ON t1.order_id = t2.order_id
GROUP BY t1.order_id
于 2013-10-19T17:10:52.720 回答
1
SELECT order_id,GROUP_CONCAT(customer_name SEPARATOR ',')as customer_name,
GROUP_CONCAT(`fone no.` SEPARATOR ',')as `fone no.` 
FROM order_management o JOIN order_management2 o2 
ON o.order_id=o2.order_id GROUP BY o.order_id      
于 2013-10-19T17:01:41.327 回答
0

你可以试试这个查询:

SELECT order_id, GROUP_CONCAT(customer_name SEPARATOR ', ') cname, GROUP_CONCAT(fone_no SEPARATOR ', ') fno
FROM order_management o1
    LEFT JOIN order_management2 o2 ON o1.order_id = o2.order_id
GROUP BY o1.order_id

这样,您应该会收到请求的结果。

于 2013-10-19T17:06:17.447 回答