我有两个查询,我想在SQL中加入第二个查询和第二个查询,这是我的查询,
第一次查询
OleDbCommand Query1 = new OleDbCommand();
Query.CommandText = "SELECT [Mastertable].ID " +
"FROM [Mastertable] INNER JOIN [relationship] ON [Mastertable].ID = [relationship].SW1_ID " +
"WHERE ([Mastertable].[PN]= @PN AND [Mastertable].[Ver]=@Ver) " +
"GROUP BY [Mastertable].[ID]";
DataSet ResultSet = RunQuery(Query1);
以上查询基本返回ID为PN = PN输入和Ver =版本输入。(我把结果放在数据集中
我想从query1加入ID列到Mastertable。 ID再次
第二个查询,
OleDbCommand Query2 = new OleDbCommand();
Query2.CommandText = "SELECT [Mastertable].PN, [Mastertable].[Ver], [relationship].[Category], [Mastertable].[Component_Name] " +
"FROM (Query1 INNER JOIN [relationship] ON Query1.[ID] = [relationship].SID) INNER JOIN " +
"[Mastertable] ON [relationship].SW2_ID = [Mastertable].[ID]";
DataSet ResultSet2 = RunQuery(Query2);
但是这段代码不起作用,我在网上搜索了很多并尝试了一些,但他们没有帮助。我正在考虑使用ResultSet1.Tables [0] .TableName和ResultSet1.Tables [0] .Columns [0] .Name,但这不起作用=(
请帮忙
答案 0 :(得分:1)
您可以将Query1作为派生表连接到第二个(外部)Query2,如下所示:
SELECT [SW Mastertable].SW_PN, [SW Mastertable].[SW_Ver],
[SW relationship].[SW2_Category], [SW Mastertable].[Component_Name]
FROM
(SELECT [Mastertable].ID
FROM [Mastertable]
INNER JOIN [relationship] ON [Mastertable].ID = [relationship].SW1_ID
WHERE ([Mastertable].[PN]= @PN AND [Mastertable].[Ver]=@Ver)
GROUP BY [Mastertable].[ID]
) Query1
INNER JOIN [SW relationship] ON Query1.[ID] = [SW relationship].SW1_ID)
INNER JOIN [SW Mastertable] ON [SW relationship].SW2_ID = [SW Mastertable].[ID];
如果您使用的是sql-server
,您也可以使用CTE执行此操作:
WITH Query1 AS
(
SELECT [Mastertable].ID
FROM [Mastertable]
INNER JOIN [relationship] ON [Mastertable].ID = [relationship].SW1_ID
WHERE ([Mastertable].[PN]= @PN AND [Mastertable].[Ver]=@Ver)
GROUP BY [Mastertable].[ID]
)
SELECT [SW Mastertable].SW_PN, [SW Mastertable].[SW_Ver],
[SW relationship].[SW2_Category], [SW Mastertable].[Component_Name]
FROM
Query1
INNER JOIN [SW relationship] ON Query1.[ID] = [SW relationship].SW1_ID)
INNER JOIN [SW Mastertable] ON [SW relationship].SW2_ID = [SW Mastertable].[ID];
答案 1 :(得分:0)
不确定这是否有效,但你仍然可以尝试一下。如果有的话,请修复语法错误。
此外,我假设您在尝试创建Query2对象时可以使用Query1对象(即不为null或不在范围之外)。
OleDbCommand Query2 = new OleDbCommand();
Query2.CommandText = "SELECT [SW Mastertable].SW_PN, [SW Mastertable].[SW_Ver], [SW relationship].[SW2_Category], [SW Mastertable].[Component_Name], [SW relationship].SW2_ID " +
"FROM (("+ Query1.CommandText +")TAB1 INNER JOIN [SW relationship] ON TAB1.[ID] = [SW relationship].SW1_ID) TAB2 INNER JOIN " +
"[SW Mastertable] ON [TAB2].SW2_ID = [SW Mastertable].[ID]";
DataSet ResultSet2 = RunQuery(Query2);
希望这有帮助。
答案 2 :(得分:0)
OleDbCommand Query2 = new OleDbCommand();
Query2.CommandText = "SELECT [SW Mastertable].SW_PN, [SW Mastertable].[SW_Ver], [SW relationship].[SW2_Category]," +
" [SW Mastertable].[Component_Name] " +
" FROM " +
"(SELECT [Mastertable].ID as ID " +
"FROM [Mastertable] INNER JOIN [relationship] ON [Mastertable].ID = [relationship].SW1_ID " +
"WHERE ([Mastertable].[PN]= @PN AND [Mastertable].[Ver]=@Ver) " +
"GROUP BY [Mastertable].[ID]) as Query1 " +
" INNER JOIN [SW relationship] ON Query1.[ID] = [SW relationship].SW1_ID INNER JOIN " +
"[SW Mastertable] ON [SW relationship].SW2_ID = [SW Mastertable].[ID]";
DataSet ResultSet2 = RunQuery(Query2);