作者cutecpu (可愛中央處理器)
看板Database
標題Re: [SQL ] 子查詢併成一欄
時間Mon Aug 22 15:13:56 2011
select t1.ID,t1.NAME,
stuff(
(
select ',' + city
from B t2
where t2.ID = t1.ID
for xml path('')
),1,1,''
) as city
from A t1
※ 引述《fr730149 ()》之銘言:
: ※ 引述《cutecpu (可愛中央處理器)》之銘言:
: : select A.ID,A.NAME,(
: : select city + ','
: : from A t2
: : where t2.ID = t1.ID
: : for xml path('')
: : ) as city
: : from A t1
: 非常謝謝您的回答,照你所撰寫的語法,似乎有誤…
: 所以我將您的語法,改寫成以下,就可順利執行:
: -----------------------------------------------
: select t1.ID,t1.NAME,(
: select city + ','
: from B t2
: where t2.ID = t1.ID
: for xml path('')
: ) as city
: from A t1
: -----------------------------------------------
: 但現在又出現一個問題,找出來的資料都會在,字尾加上','
: 如下欄city所示:
: ID name city
: 1 Mark Taipei,Tainan,
: 2 Mary Taipei,
: 3 Leo ChangHua,Kaohsiung,
: 目前我的作法是在,程式端將最後一個','刪掉。
: 那麼是否有方法:單用SQL語法將','刪除
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.59.164.109
推 fr730149:原來還有stuff可用 再次感謝 08/22 15:20
推 koehie:那如果在 Oracle 呢 ? 同樣的需求要如何解決呢 ? 08/28 21:20