我有两张桌子
1)事故
INCIDENT_GID NAME
------------------ -----------------------------
178175721842183084 KENNEDY,KELLY
178175721842183084 GRESLY,ANTHONY
2)Incident_crew
INCIDENT GID NAME
------------- ------------------ ------------------------------
INC 1121035756 178175721842183084 KENNEDY, KELLY;GRESLY, ANTHONY
事件有一个名为GID的主列,Incident_crew引用名为Incident_GID的此GID。 Incident_crew有两行或更多行,它们引用相同的Incident_GID,因此返回两行以上。在Incident_crew表中,我有一个名为NAME的列,我希望将多个行中的NAME组合起来,并将其作为一行返回,此处由半色谱图像描述分隔。
结果应如下所示:
$output = [];
array_map(function (...$keys) use (&$output) {
// Pop the last key, because it is actually a value.
$value = array_pop($keys);
// Prepare "element" to assign the value to using keys and references.
$element = &$output;
while($key = array_shift($keys)) {
if (!isset($element[$key])) {
$element[$key] = [];
}
$element = &$element[$key];
}
$element[] = $value;
}, ...array_map(function ($group) use ($results) {
return array_column($results, $group);
}, $groups));
答案 0 :(得分:0)
SELECT INCIDENT, GID, NAME = STUFF(NAME, 1, 1, '')
FROM Incident i
CROSS APPLY
(
SELECT ';' + ic.NAME
FROM Incident_crew ic
WHERE ic.INCIDENT_GID = i.GID
FOR XML PATH('')
) n (NAME)