SQL连接返回

时间:2014-04-17 23:23:21

标签: sql return comma

我有两个表:帐户和财产。 Accounts表有一个名为“accountID”的字段。 Property表包含名为“accountID”和“propertyName”的字段。

我需要在逗号分隔的连续列中返回所有帐户ID及其所有相关属性的列表...例如,我有3个帐户ID:101,102,103

accountID 101 has three properties: house, car, boat
accountID 102 has four properties: plane, yacht, motorcycle oyster
accountId 103 has one property: fish

我需要返回看起来像:

accountID   properties
101         house, car, boat
102         plane, yacht, motorcycle, oyster
103         fish

1 个答案:

答案 0 :(得分:0)

这样的东西?

SELECT `accountID`, GROUP_CONCAT(`properyName` SEPARATOR ', ') as `properties`
FROM `Accounts`
JOIN `Property` USING (`accountID`)
GROUP BY `accountID`