MS 访问 SQL 查询
Posted
技术标签:
【中文标题】MS 访问 SQL 查询【英文标题】:MS Access to SQL Query 【发布时间】:2014-10-28 21:44:56 【问题描述】:我有几个查询已从 Access DB 迁移到 SQL Server。然而结果并不相同。以下是 Access 和 SQL 中的查询。如果查询有任何问题,谁能告诉我。
Query 1 in MS Access
SELECT [TableA].Mat, [TableA].Sg, [TableA].Pt, [TableB].[Ptype]
FROM [TableA] INNER JOIN [TableB] ON [TableA].Sg=[TableB].Sg
WHERE ((([TableA].Dl)="04") And [TableB].[Ptype]<>"dummy") - TableFinal
Query2 in MS Access
SELECT DISTINCT TableFinal.Mat, TableFinal.Sg, IIf(TableFinal.Pt Is Null,[TableD].Pt,TableFinal.Pt) AS Pnt, [TableC].[Ptype]
FROM (TableFinal INNER JOIN [TableC] ON TableFinal.Sg=[TableC].Sg) INNER JOIN [TableD] ON ([TableC].Pt=[TableD].Pt) AND (TableFinal.Mat=[TableD].Mat)
WHERE ((([TableC].[Ptype])<>"dummy"));
Combined Query1&2 in SQL
SELECT DISTINCT [TableA].Mat, [TableA].Sg, ISNULL([TableA].Pt, [TableC].Pt) AS Pln,
[TableB].[Ptype]
FROM [TableA] INNER JOIN
[TableB] ON [TableA].Sg = [TableB].Sg INNER JOIN
[TableC] ON [TableB].Pt = [TableC].Pt AND
[TableA].Mat = [TableC].Mat
WHERE ([TableB].[Ptype] <> 'dummy') AND ([TableA].Dl = '04') - Final
Query3 in MS Access
SELECT DISTINCT Final.Mat, Final.Sg, Final.Pt, IIf([TableB].[Ptype] Is Null,Final.[Ptype],[TableB].[Ptype]) AS [Pan]
INTO Destination
FROM [TableB] RIGHT JOIN Final ON ([TableB].Pt=Final.Pt) AND ([TableB].Sg=Final.Sg);
Query3 in SQL
SELECT DISTINCT Final.Mat, Final.Sg, Final.Pt,
ISNULL(TableB].[Ptype], Final.[Ptype]) AS [Ptype]
FROM TableB] RIGHT OUTER JOIN
Final ON [TableB].Pt = Final.Pt AND
[TableB].Sg = Final.Sg
【问题讨论】:
当我在 SQL 中使用已编辑的查询时,没有显示任何数据。我担心的是我是否以正确的方式编辑了查询。 【参考方案1】:我不确定,但我认为这是您的意图:
--Combined Query1&2 in SQL
SELECT DISTINCT
[TableA].Mat,
[TableA].Sg,
ISNULL([TableA].Pt, [TableC].Pt) AS Pln,
[TableB].[Ptype]
INTO
#FINAL
FROM
[TableA]
INNER JOIN [TableB] ON [TableA].Sg = [TableB].Sg
INNER JOIN [TableC] ON [TableB].Pt = [TableC].Pt AND
[TableA].Mat = [TableC].Mat
WHERE
([TableB].[Ptype] <> 'dummy') AND
([TableA].Dl = '04')
--Query3 in SQL
SELECT DISTINCT
Final.Mat,
Final.Sg,
Final.Pln, --Final.Pt,
ISNULL([TableB].[Ptype], Final.[Ptype]) AS [Ptype]
FROM
[TableB]
RIGHT OUTER JOIN #FINAL AS Final ON [TableB].Pt = Final.Pln AND
[TableB].Sg = Final.Sg
【讨论】:
感谢我写的查询和你提供的查询是一样的。新加载数据后没有像“04”这样的值,因此它没有显示任何结果。以上是关于MS 访问 SQL 查询的主要内容,如果未能解决你的问题,请参考以下文章
使用java和SQL查询INSERT INTO将新记录插入MS访问[关闭]