/**
* 通过 FOR XML PATH 语句,可以将字段的值进行合并。
**/ CREATE TABLE tb_child
(
name VARCHAR(32),
hobby VARCHAR(32)
) go INSERT INTO tb_child
VALUES ('tim',
'爬山') INSERT INTO tb_child
VALUES ('tim',
'游泳') INSERT INTO tb_child
VALUES ('tim',
'自行存') INSERT INTO tb_child
VALUES ('joe',
'自行存') INSERT INTO tb_child
VALUES ('joe',
'弹吉他') go SELECT name,
(SELECT hobby + '^'
FROM tb_child t1
WHERE t1.name = t2.name
FOR XML path(''))
FROM tb_child t2
GROUP BY name

得到的结果如下

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATkAAABmCAIAAAA/LHEwAAAKk0lEQVR4nO2dv2vruhvG9ddcztJywFMzdbhw50I7d7loPwdK+W75D9pC6cFbhlI4UO6QA5kzlsZDh0CHkqlb6NBs3fQd5LiufrySfyiWk+fhobiyLMm2Pn4lO3HYcDicPz0IIeZPD4oZY0rKcDg8PDy05YdhOJwZwapusArDXRmswnA/DFZhuB/OWR16S7Lqnx+CoFbEhsPhIQRB0Yt1fK2AIMhP6lMZGIbjNFiF4X4YrMJwPwxWYbgfzlkVvVLnRw2GN2827+EHGw4PDztvAwxv2Kx3oM6fHobDYdeh3UudHyh4m9xXVnsRWnvRSLgvpljlaWZ0wlVvuNFGVqfT6XQ6HX/VaHTb4cGV8b/DBsDbZAeraSZyp4JbTLOqfAlW/05sDRtZTdNMGYIul0ueph0e3L7Ef7gXrhZX9Yia8CxJGrHK7LKVSbO6Wq2yN5G9ibvHd87BKrwlplgtR6rZbFYsT6dTufDx8ZGmWZJwa+lr3ggUy0zalhUTrD4/P2dZNpvN7h7f0zRryCrRBp8BAliFW7QvqwWfQojxeCxBXa1WaZodHByYi3bRWKS0FVfTLBuPx2majUa30pVY1RtsXPBvLViFWzTF6mh0+/z8PJvNpl8leZB3bmhW9U5sZNW5rNhzvnr3+J4H2CxLs2yxWLgPx1c4y3/pVoFVOLQdrBrjappmq9VKTgtHo1sbq3kFloh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