比如我有兩個表是這樣的:子查詢結果到一行
Table Parent
+===========+==============+
| FirstName | (PK)LastName |
+===========+==============+
| Bob | Newbie |
| Bella | Goth |
+===========+==============+
Table Child
+===========+==============+
| FirstName | (FK)LastName |
+===========+==============+
| Cassandra | Goth |
| Cindy | Newbie |
| Mortimer | Goth |
+===========+==============+
我應該怎麼辦有一個SELECT的結果是這樣的:
+========+============+=====================+
| Family | ParentName | ChildName |
+========+============+=====================+
| Goth | Bella | Cassandra, Mortimer |
| Newbie | Bob | Cindy |
+========+============+=====================+
是我到目前爲止已經試過
SELECT
A.LastName Family,
A.FirstName ParentName,
(
SELECT
COALESCE(B.FirstName + ',' , '') + B.FirstName
FROM
Child B
WHERE
B.LastName = A.LastName
) ChildName
FROM
Parent A
ORDER BY
Family ASC
但是當我執行它表明
子查詢返回的值超過1。當子查詢遵循=,!=,<,< =,>,> =或當子查詢用作表達式時,這是不允許的。
有什麼想法嗎?
** LIFE SAVER **,你能解釋爲什麼逗號的第一個字符被刪除嗎? – rizkynggakool
@rizkynggakool,stuff函數將長度爲1的位置1的字符替換爲空字符串。 –