-2

使用 Linq to SQL

一个查询和匹配如何包含来自辅助表的所有行,其列中包含空格?

表格1:

ID  FAQ    MODELS 
1   faq1   model1 
2   faq2   model2 model1
3   faq3   model3 model2 model1   (Spaces in models column)

表 2:

ID  MODELS PIC
1   model1 model1pic
2   model2 model2pic
3   model3 modal3pic

期待:

faq1 model1 model1pic
faq2 model1 model1pic
faq2 model2 model2pic
faq3 model1 model1pic
faq3 model2 model2pic
faq3 model3 model3pic


SELECT kwfaqtmp.faqmodelnum, kwFAQtmp.issue,
kwfaqtmp.resolution, kwtable4tmp.modelnum,     
kwtable4tmp.prodpic 
FROM kwfaqtmp AS t1 CROSS APPLY dbo.SplitStrings(t1.faqmodelnum, ' ') AS s 
INNER JOIN dbo.kwtable4tmp 
AS t2 ON s.item COLLATE SQL_Latin1_General_CP1_CI_AS = t2.modelnum 
COLLATE SQL_Latin1_General_CP1_CI_AS 
ORDER BY t1.issue;
4

3 回答 3

2

如果您无法修复设计(您不应该将多条数据存储在单个列中,正如其他地方所指出的那样),您可以使用拆分功能来完成此操作:

CREATE FUNCTION dbo.SplitStrings
(
  @List  NVARCHAR(MAX),
  @Delim NCHAR(1)
)
RETURNS TABLE
AS
   RETURN ( SELECT DISTINCT Item FROM
       ( SELECT Item = x.i.value('(./text())[1]', 'nvarchar(max)')
         FROM ( SELECT [XML] = CONVERT(XML, '<i>'
         + REPLACE(@List, @Delim, '</i><i>') + '</i>').query('.')
           ) AS a CROSS APPLY [XML].nodes('i') AS x(i) ) AS y
       WHERE Item IS NOT NULL
   );
GO

现在你可以说:

SELECT t1.FAQ, t2.MODELS, t2.PIC
FROM dbo.Table1 AS t1
CROSS APPLY dbo.SplitStrings(t1.MODELS, ' ') AS s
INNER JOIN dbo.Table2 AS t2
ON     s.Item COLLATE SQL_Latin1_General_CP1_CI_AS 
  = t2.Models COLLATE SQL_Latin1_General_CP1_CI_AS
ORDER BY t1.FAQ;

结果:

FAQ   MODELS  PIC
----  ------  ---------
faq1  model1  model1pic
faq2  model1  model1pic
faq2  model2  model2pic
faq3  model1  model1pic
faq3  model2  model2pic
faq3  model3  modal3pic

您尝试使用的查询:

 SELECT kwfaqtmp.faqmodelnum, kwFAQtmp.issue,kwfaqtmp.resolution, 
    kwtable4tmp.modelnum, kwtable4tmp.prodpic 
 FROM kwfaqtmp AS t1 
 CROSS APPLY dbo.SplitStrings(t1.faqmodelnum, ' ') AS s 
 INNER JOIN dbo.kwtable4tmp AS t2 
   ON s.item COLLATE SQL_Latin1_General_CP1_CI_AS 
     = t2.modelnum COLLATE SQL_Latin1_General_CP1_CI_AS 
 ORDER BY t1.issue;

根本无效。尽管它不应该产生您在评论中引用的确切错误。保持 t1 / t2 别名作为我的原始查询显示如何?

 SELECT t1.faqmodelnum, t1.issue, t1.resolution, 
    t2.modelnum, t2.prodpic 
 FROM kwfaqtmp AS t1 
 CROSS APPLY dbo.SplitStrings(t1.faqmodelnum, ' ') AS s 
 INNER JOIN dbo.kwtable4tmp AS t2 
   ON s.item COLLATE SQL_Latin1_General_CP1_CI_AS 
     = t2.modelnum COLLATE SQL_Latin1_General_CP1_CI_AS 
 ORDER BY t1.issue;
于 2012-05-16T18:19:39.173 回答
0

您应该在映射类中使用ColumnAttribute。创建一个带有不带空格的字段名的 POCO 类,映射到表中带空格的类。

于 2012-05-16T18:25:30.863 回答
0

我认为你需要重构你的数据库。

FAQ (remove MODELS column)

ID  FAQ
1   faq1
2   faq2
3   faq3

FAQMODELS (Create this table)
ID FAQ   MODELS
1  faq1  model1
2  faq2  model2
3  faq2  model1
4  faq3  model3
5  faq3  model2
6  faq3  model1

MODELSPIC    
ID  MODELS PIC
1   model1 model1pic
2   model2 model2pic
3   model3 modal3pic

那么你就可以

SELECT
    FAQ, 
    MODELS, 
    PIC 
FROM FAQMODELS INNER JOIN MODELSPIC ON FAQMODELS.MODELS = MODELSPIC.MODELS
于 2012-05-16T18:31:47.863 回答