0

我终于找到了要执行的查询,以获取一行中一个内容的所有 ID(逗号分隔)。

以下查询成功了:

您不需要查看查询,因为它已经做了它应该做的事情。

SELECT 
    taxonomy_item_id, 
    SUBSTRING(
      (SELECT ', ' + CAST(taxonomy_id AS varchar) AS Expr1
       FROM taxonomy_item_tbl AS t2
       WHERE (t1.taxonomy_item_id = taxonomy_item_id) AND (taxonomy_language_id = 2067)
       ORDER BY taxonomy_item_id, taxonomy_id FOR XML PATH('')
      ), 1, 1000) AS taxonomy_ids
FROM 
    taxonomy_item_tbl AS t1
WHERE 
    (taxonomy_language_id = 2067) AND (taxonomy_item_id = 180555)
GROUP BY 
    taxonomy_item_id

唯一的问题是我得到的数据结果:

180555  |   <Expr1>, 404</Expr1><Expr1>, 405</Expr1><Expr1>, 723</Expr1><Expr1>, 1086</Expr1><Expr1>, 1087</Expr1><Expr1>, 1118</Expr1><Expr1>, 1124</Expr1><Expr1>, 1126</Expr1>

我不需要<Expr1>节点。有没有办法删除这个?如果我在查询中删除AS Expr1,那么它会自动添加回来

谢谢

4

1 回答 1

0

如果你不想要<Expr1>- 那么就不要要求它!

你有:

(SELECT ', ' + CAST(taxonomy_id AS varchar) AS Expr1

AS Expr1会导致<Expr1>添加 - 所以只是没有那个表达式。

尝试

SELECT 
    taxonomy_item_id, 
    SUBSTRING(
      (SELECT ', ' + CAST(taxonomy_id AS VARCHAR) 
       FROM dbo.taxonomy_item_tbl AS t2
       WHERE t1.taxonomy_item_id = taxonomy_item_id
       AND taxonomy_language_id = 2067
       ORDER BY taxonomy_item_id, taxonomy_id 
       FOR XML PATH('')
      ), 1, 1000) AS taxonomy_ids
FROM 
    dbo.taxonomy_item_tbl AS t1
WHERE 
    taxonomy_language_id = 2067
    AND taxonomy_item_id = 180555
GROUP BY 
    taxonomy_item_id
于 2011-12-12T17:02:33.953 回答