将行结果拆分为列
问题描述:
我正在尝试创建一个查询,从我们的数据库中提取所有客户信息,但由于 1 家公司背后通常有多个联系人,因此我将同一家公司列在多行中.我希望每个公司只出现在 1 行中,而将其他联系人放在新列中,
I am trying to create a query that pulls all customer info from our database, but because there are often multiple contact persons behind 1 company, I get the same company listed in multiple rows. I would like to have each company only show up in 1 row and place additional contact persons in new columns instead,
当前结果:
Company | Contact person
-------------------------------
Company 1 | Alex
Company 1 | Tom
Company 2 | James
Company 2 | Andy
预期结果:
Company | Contact person 1 | Contact person 2
----------------------------------------------
Company 1 | Alex | Tom
Company 2 | James | Andy
这里是大约.我的查询是什么样的:
Here is approx. what my query looks like:
SELECT Customer.Company, CustomerAdditionalInfo.ContactPerson
FROM Customer LEFT OUTER JOIN CustomerAdditionalInfo
感谢您的时间,
答
试试这个...
SELECT Customer.Company,GROUP_CONCAT(Customer.ContactPerson) AS 'AllContactPerson'
FROM Customer GROUP BY Customer.Company
这将为您提供以下结果(如果 ContactPerson 详细信息来自 Customer
表.)
this will give you as under result (if ContactPerson details comes from Customer
table. )
Company | AllContactPerson
--------------------------------
Company 1 | Alex,Tom