首页 > 解决方案 > mysql和php中fetech如何数据一对多关系

问题描述

我有一个表客户和第二个表订单。我已经从客户那里获取了,但它获取了重复的记录。

select customers.name, customers.mobile, orders.Product
from customers,
     orders
where customers.id = orders.customer_id

结果 :

在此处输入图像描述

目标 :

在此处输入图像描述

标签: mysqlsql

解决方案


你需要在GROUP BYCustomersOrder专栏

select c.name,
       c.mobile,
       group_concat(o.Product) #products name separated by a comma
from customers c
left join orders o on
  c.id = o.customer_id
group by c.id

我不熟悉你的结构,但我想这就是你想要了解的要点


推荐阅读