Sql server—— for xml path简单用法(可以按照分组把相同组的列中的不同的值,像字符串一样拼接在一起显示在分组之后的列中。)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAI8AAACWCAIAAABo2EyXAAAKeklEQVR4nO2dy27rNh7G+U7CFIrfZXDQOOCDzM62DpCNA3DnRZDNtB3HA2TtdUygGMeJiwZue5rT046Tg9hJfIkv0Cwoy9SFulEUxTE/EAbDSPxL/JmUok9kQKvVmmgpIqBpKSRNSyVpWirJoVWzmoom2Q1YqPa0bAWlaakk6T274N6vPK1NuVU0LYhwaDKgPxXJiahmNV+86na73W6341Wrdf4iQ3JoIWw7CdmQkaJpAQAifsyRFkL+wxiPxxChA6IV26sMiA2DixZgK5rW1CuX1nQ6xU82frIvricQoqkMrdfromnR39Zer+fmu90uySyXS4SwYcBYVBEwaCqsfCgt3/mQox0OhxjjXq93cT1BCEOIuNoIgAy/mkwmkmm5hGzb7nQ6BBX5Rh8dHcWiimCQuW89e4UQRhh3Oh2EcKt1ThKE6DmxAABJMizRVUmg1WqdD4fDXq/X9Yq0CLmGR9MKNnoordh8KK2vXgWvWxfXE6eTYYwwHo1GX+MEAKAz9KdvA9aORKvVSgItN0/3LYQwGZ3xk91qnbNo+Rqd1W8y960nrxDCDw8P4/F4PB5jbF9cTy6uJ/QxGwYcjUZPaQQACJawRG8mgVboSLhcLjudjnslj6YV3eI829es5qNXCOHRaOQZADAmp2EjiBDu9XoE2GOYAAB0PqjgZqzdHx8fJdA6OzvDGJOTdy8J5NO9NiCEYkdCwiC030R8VaO7V81qjrwKjoQOLdu2bXs0GhkQGgYkwEIFAPBlQn/FEr39fD6XQGt/5giT24rpdNpqneMnm4w2Z2dnEfeEPnihebqE/oxWzWree0VokU4/Ho/x044WgjaEtm1DZCCEDQjv4xQKw/0Vaxf6R8m0yOjnXqvcC0MsLbpvsWiBwN1jLLCa1fzZK5cWQYWfPCP5dDodjUYIY8OAP8cJAMAqiehb9PZyaLl3gPToR24Fz3Zi0fINZaFDX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hAs4VUZaTFW71/066Y4p9/9eyu2VePvMiqViu8uwy3hUxlpaae/hKG1069SaO30qxRaO/0qhdZOv0qhtdOfRaWjJS5kLpVLbDJNS04lyoXWtFQKrWmpFDo5LXqxR1HzjnlX7y9EqtASu0J3Hk91i5AKtPKbZJjkbDM6JoWojLT8Tv/tR9fhEu708/Stva9P/VPqXBfWLSMtv9PfPtmtBC96ljjH6v10n3SywRJeqUArzPUX9V4Gz+r9Hou7cnoVKPm/nHcsZU5/Dqv3+0bQvBaBjw1diDLM6b88Bmb9RsCcfv7V+wtZvr+MtIqf05/D6v2eatwKkr8/kEhlpKWd/hKG1k6/SqG1069SaO30qxRaO/0qhdZOf2rJQsWkJfGAtCKkaakkTUslaVoqSdNSSZqWStK0VJKmpZL+B9uc1QxyqGM7AAAAAElFTkSuQmCC" alt="" />

一个表,然后语句如下

--查询
select
id,
name=stuff((select ','+name from tb where id=t.id for xml path('')),1,1,'')
from
tb t
group by
id

结果如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHwAAABeCAIAAABuGOfUAAAFoElEQVR4nO2d326bSBTG56W2muVd9mJN5AfZu2Ii+aav0JvudnFX8mus5D+5sbRt2jTtXqxC3BjbGIu9AJyBYWBg5jDYOZ9QRCZw5pwf48HmA4f4vv/afRNnevfHXzEKWAShdy+EbkAI3YAQugGVQMcFeilC91HwQugGhNANCOf0Hszp74DfvazX654EMZgDQjeQQ9fQHx8fexLEYA4I3UAO/YO+GlvWeKUYBF7nBN33/dzvPGIJ6MUgJqSSQ9fQHx4earaQgF4fBF4qOZRAPxyi7S6EWKLo+O37v7n4y2tKrxfJCsmUtDQMMrLTve1JuN2Fnn0KR+xJss2VTQkhhLofRunKzTbX9c+jZYNCdEIPD1Gw3UMsUXT8ev8tF3/hUurOtzcOJQNvH2z3gXdFqDtvGoRku6cB98UuFi4l1FkkG2cradfUWQj2rczhPKAfj8e7r/cl0Nlq6yoXBimsL1zmpcN0UVhhNyOEkOwASBSiFXp42AQ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AMJIHQDCSB0Awk0tusUHzqqSFe+i/qagQ2NTqA/366t+qyXMN0mXbwM6M9SzV0i3fouLhO60K5bXlO1B+ykeLUe6fLPHKkJBLrQOTp95yScc9R+TmcOVzpZ8S161B30YLsP2K8KhfFI9dyCUescqQkGuvirGWcOpc4Swq47ScMtGBcAfeZQMphsgt2G/epIrdA13GzE7JjZrXyLHoFA55+uOzl2Kl5dhV0Xy3tlMidSy7IKJ9JTiw6BQL9ou06DYKCjXQeZANp1BhJAu85AAmjXGUgA7bo2Ujxm+J93DQihGxBCNyCEbkAI3YAQugEhdANC6Ab0P1R/vpADcIxRAAAAAElFTkSuQmCC" alt="" />

分组的时候 注意看清 id=t.id  。错误的情况会把所有的列加起来。非常尴尬。

上一篇:在 Android开发中,性能优化策略十分重要


下一篇:Struts2学习笔记三 访问servlet