我有2个表格order_management
和order_management2
几乎相同... order_id
order_management
自动递增其中{ {1}}中{1}}没有。我想根据order_id
合并两个表中每个字段的数据。
假设,
order_management table
order_management2
order_management2表
order_id
如果两个表的order_id customer_name fone no.
-------- ------------- ---------
1 A 89000
2 B 78989
3 C 56464
为 order_id customer_name fone no.
---------- --------------- ----------
2 D 89076
3 E 67598
4 F 67545
,那么它应显示为:
order_id
怎么做?
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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
通过这个你应该收到要求的结果。