简体   繁体   English

如何在单个sql表中合并两个记录的相同字段

[英]How to merge same field for two records in single sql table

Am maintaining a data base named CustomerInfo for multiple employees, whenever a customer contacts me am inserting that customer details into the table.... If the same customer calls again and again, am inserting as new record with only id number and product for which the call made is differed... 正在维护一个用于多个员工的名为CustomerInfo的数据库,每当有客户与我联系时,便将该客户详细信息插入表中。拨打的电话有所不同...

If i search table with id it returns only one record, but if i search with phone number or email id it may give multiple record, Now i want the return record as follows 如果我用id搜索表,它只返回一条记录,但是如果我用电话号码或电子邮件id搜索,它可能给出多条记录,现在我要返回记录如下

Customer Name, Phone Number, Email Id, product1,product2,...

Is it possible to do like this in SQL...? 是否可以在SQL中这样做? If so please explain me... 如果是这样,请向我解释...

You can do this with aggregation and group_concat() : 您可以使用aggregation和group_concat()做到这一点:

select CustomerName, PhoneNumber, EmailId, group_concat(product)
from CustomerInfo
group by CustomerName, PhoneNumber, EmailId;

That said, your database should have at least two tables. 也就是说,您的数据库应该至少有两个表。 One for customers with their contact information. 一个供客户使用的联系方式。 And another, CustomerProducts for the product information. 另一个是用于产品信息的CustomerProducts This is called "normalization" and you should be familiar with this concept if you are using relational databases. 这称为“规范化”,如果您使用关系数据库,则应该熟悉此概念。 There are many good explanations online. 在线上有很多很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM