我似乎总是被多个联接所困扰。我有这些包含部件号的表,然后这些部件号的描述分布在12个表中(我对此无能为力)。所以我想只选择我需要的部件号,然后通过将所有12个表与原始查询相加来获取它们的描述。
结果是只有第一个表的描述,电容器才会加入。其余的都丢失了,我可以注释掉电容器并获得连接器,依此类推,但我一次只能得到一个。
有没有办法在SQL中执行此操作?或者我错过了船。
这是我的代码:
cmd.CommandText = "WITH PARTS AS(";
cmd.CommandText += "SELECT *, ROW_NUMBER() OVER (ORDER BY p.PART_NUMBER) AS 'RowNumber' FROM dbo.AllParts AS p";
cmd.CommandText += ")";
cmd.CommandText += "SELECT * FROM PARTS as p "
+ "INNER JOIN dbo.[Component Manufacturer Parts] AS a ON p.RowID = a.PartGUID "
+ "INNER JOIN dbo.[Manufacturer Part Info] AS b ON a.ManufacturerGUID = b.RowID "
+ "LEFT Join dbo.[CAPACITORS] as caps ON caps.RowID = p.RowID "
+ "LEFT Join dbo.[Connectors] as cons ON cons.RowID = p.RowID "
+ "LEFT Join dbo.[Crystals and Oscillators] as xtal ON xtal.RowID = p.RowID "
+ "LEFT Join dbo.[Diodes] as dio ON dio.RowID = p.RowID "
+ "LEFT Join dbo.[ICs] as ics ON ics.RowID = p.RowID "
+ "LEFT Join dbo.[Inductors] as ind ON ind.RowID = p.RowID "
+ "LEFT Join dbo.[Misc] as mis ON mis.RowID = p.RowID "
+ "LEFT Join dbo.[Relays] as rel ON rel.RowID = p.RowID "
+ "LEFT Join dbo.[Resistors] as res ON res.RowID = p.RowID "
+ "LEFT Join dbo.[Switches] as swi ON swi.RowID = p.RowID "
+ "LEFT Join dbo.[Transformers] as tra ON tra.RowID = p.RowID "
+ "LEFT Join dbo.[Transistors] as xfrm ON xfrm.RowID = p.RowID "
+ "WHERE RowNumber Between @low AND @high AND p.PART_NUMBER LIKE 'CT%'";
感谢您提供任何建议。
答案 0 :(得分:1)
它不起作用的原因是因为第一个左连接没有返回任何与第二个左连接匹配的内容。
视图中包含大联盟中所有表的描述。它非常丑陋,但查询肯定会更容易。