我目前在导出 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 列指定列。