2013-08-20 66 views
1

導出1時,我當前加入了2個其他表格,但這導致行被複制。而不是複製行以匹配值,是否可以用逗號分隔特定行的值?加入表格而不復制該行

這裏是我的表的樣本,因爲它現在是:

id,optioncatid,optionsdesc_sidenote,isproductcode,applytoproductcodes,stockstatus 
"325","30","","BRB8PACK","00LDCLU131401C","17" 
"325","30","","BRB8PACK","00LDDEV131401C","17" 
"325","30","","BRB8PACK","00LDHEI131401C","17" 
//etc 

這是我想它是:

id,optioncatid,optionsdesc_sidenote,isproductcode,applytoproductcodes,stockstatus 
"325","30","","BRB8PACK","00LDCLU131401C,00LDCLU131401C,00LDHEI131401C, etc...","17" 
//etc 

可以有成千上萬值的applytoproductcodes,當以XML格式導出時,將文件擴展到200 MB以上。這顯然非常臃腫。 我的SQL查詢:

SELECT 
    Options.ID, 
    Options.OptionCatID, 
    Options.optionsdesc_sidenote, 
    Options.IsProductCode, 
    Options_ApplyTo.ProductCode AS ApplyToProductCodes, 
    Products.StockStatus AS StockStatus 
FROM 
    Options 
JOIN Options_ApplyTo ON Options.ID = Options_ApplyTo.OptionID 
JOIN Products ON Options.IsProductCode = Products.ProductCode 
WHERE 
    Options.IsProductCode <> '' 
ORDER BY 
    Options.ID 

編輯:現在我已經做了更多的研究和修改我的代碼如下:

SELECT 
    Options.ID, 
    Options.OptionCatID, 
    Options.optionsdesc_sidenote, 
    Options.IsProductCode, 
    t.ProductCode AS ApplyToProductCodes, 
    Products.StockStatus AS StockStatus 
FROM 
Options 
LEFT JOIN 
    (
     select OptA.ProductCode as ProductCode, OptA.OptionID as OptionID 
     from Options_ApplyTo AS OptA, Options 
     WHERE Options.ID = OptA.OptionID 
     order by OptA.OptionID 
     for xml path('') 
    ) t 
ON Options.ID = t.OptionID 
LEFT JOIN Products 
ON Options.IsProductCode = Products.ProductCode 
WHERE 
    Options.IsProductCode <> '' 
ORDER BY 
    Options.ID 

但現在我得到的是沒有指定列錯誤't'的第1列。

回答

1

只需將該吸盤放入選擇。您無法在加入作爲for xml變成結果爲標值......不是表....

SELECT 
    Options.ID, 
    Options.OptionCatID, 
    Options.optionsdesc_sidenote, 
    Options.IsProductCode, 
    (
     select OptA.ProductCode as ProductCode, OptA.OptionID as OptionID 
     from Options_ApplyTo AS OptA 
     WHERE Options.ID = OptA.OptionID 
     order by OptA.OptionID 
     for xml path('') 
    ) as ApplyToProductCodes, 
    Products.StockStatus AS StockStatus 
FROM 
Options 
LEFT JOIN Products 
ON Options.IsProductCode = Products.ProductCode 
WHERE 
    Options.IsProductCode <> '' 
ORDER BY 
    Options.ID 
+0

所有的好和工作。但是現在我嘗試添加到'WHERE'部分'(StockStatus <='0'AND ApplyToProductCodes <>''),並且我得到了「無效的列名'ApplyToProductCodes'」。我如何參考? –

+0

@Jack Cole爲了在where子句中引用'ApplyToProductCodes',你必須圍繞這個查詢包含另一個選擇。 where子句不知道它的關聯select子句,因爲它在select之前執行。 – dotjoe

0

mysql GROUP_CONCAT函數將一列與逗號字符連接起來。因此,您的查詢可以寫成

SELECT 
    Options.ID, 
    Options.OptionCatID, 
    Options.optionsdesc_sidenote, 
    Options.IsProductCode, 
    t.pcodes AS ApplyToProductCodes, 
    Products.StockStatus AS StockStatus 
FROM 
OPTIONS 
JOIN 
(SELECT Options_ApplyTo.OptionID, GROUP_CONCAT(Options_ApplyTo.ProductCode) pcodes  
    FROM Options_ApplyTo GROUP BY Options_ApplyTo.OptionID) t 
ON Options.ID = t.OptionID 
JOIN Products 
ON Options.IsProductCode = Products.ProductCode 
WHERE 
    Options.IsProductCode <> '' 
ORDER BY 
    Options.ID 
+0

我使用Microsoft SQL Server 2005(9.00.4035.00),我得到的錯誤* 'GROUP_CONCAT'不是公認的內置函數名稱。* –

+0

抱歉。我認爲這是關於mysql – rKasun

0

對不起,可能是我缺乏經驗,我覺得提供的信息是不夠的,我寫的測試數據爲U,但我可以給ü的想法,使用STUFF

DECLARE @t1 TABLE 
(
id INT, 
optioncatid INT, 
optionsdesc_sidenote NVARCHAR(255), 
isproductcode NVARCHAR(255), 
applytoproductcodes INT 
) 


INSERT INTO @t1 VALUES 
(325,30,'BRB8PACK','00LDCLU131401C',17), 
(325,30,'BRB8PACK','00LDCLU131401C',17), 
(325,30,'BRB8PACK','00LDCLU131401C',17) 



SELECT id,optioncatid, 
STUFF((SELECT ','+optionsdesc_sidenote 
FROM @t1 WHERE id=325 FOR XML PATH('')) , 1 , 1 , ''), 
optionsdesc_sidenote, 
isproductcode, 
applytoproductcodes FROM @t1