我在Access 2007中有3个表,其中一个是涡轮泵列表,并且有一个主键[Numérointerne]。我正在尝试进行一个查询,该查询将该表连接到每个类型的最新操作,关于其他两个表中列出的泵。
加入2个表所产生的记录集仍然可以更新:
SELECT t.*, n.[Numéro attribué]
FROM Turbopompes AS t
INNER JOIN (
SELECT Turbopompe, [Date attribution], [Numéro attribué]
FROM [Turbopompes Numéros]
ORDER BY [Date attribution] DESC
) AS n ON t.[Numéro interne] = n.Turbopompe;
但无论我如何表达它,加入3个表都会使结果为只读:
SELECT t.*, n.[Numéro attribué], m.[Date maintenance], m.Détails
FROM (
Turbopompes AS t
INNER JOIN (
SELECT Turbopompe, [Date attribution], [Numéro attribué]
FROM [Turbopompes Numéros]
ORDER BY [Date attribution] DESC
) AS n ON t.[Numéro interne] = n.Turbopompe
) INNER JOIN (
SELECT Turbopompe, [Date maintenance], Détails
FROM [Turbopompes Maintenances]
ORDER BY [Date maintenance] DESC
) AS m ON t.[Numéro interne] = m.Turbopompe;
我尝试过的另一个查询结果相同:
SELECT t.*, n.[Numéro attribué], m.[Date maintenance], m.Détails
FROM (
Turbopompes AS t
INNER JOIN (
SELECT Turbopompe, [Date attribution], [Numéro attribué]
FROM [Turbopompes Numéros]
ORDER BY [Date attribution] DESC
) AS n ON t.[Numéro interne] = n.Turbopompe
) INNER JOIN (
Turbopompes AS t2
INNER JOIN (
SELECT Turbopompe, [Date maintenance], Détails
FROM [Turbopompes Maintenances]
ORDER BY [Date maintenance] DESC
) AS m ON t2.[Numéro interne] = m.Turbopompe
) ON t.[Numéro interne] = t2.[Numéro interne];
自:
http://www.fmsinc.com/Microsoftaccess/query/non-updateable/index.html
您的数据可能无法更新的原因有很多。 :
- 具有不在关键字段上的多表连接的查询
因此Turbopompes和[TurbopompesNuméros]之间的内部连接不保留主键?有没有办法解决这个问题?
答案 0 :(得分:1)
对于连接表达式的嵌套,访问有点奇怪。 字段“Turbopompe”必须是表N和表M中的主键。提供的,您应该使用此查询获得所需的,可更新的结果:
SELECT t.*, n.[Numéro attribué], m.[Date maintenance], m.[Détails]
FROM ( Turbopompes AS t
INNER JOIN [Turbopompes Numéros] AS n
ON t.[Numéro interne] = n.Turbopompe )
INNER JOIN [Turbopompes Maintenances] AS m
ON t.[Numéro interne] = m.Turbopompe
ORDER BY n.[Date attribution] DESC,
m.[Date maintenance];
但如果表N和M包含每个[Numérointerne]的多个记录,则需要先查询每个“Turbopompe”的最新记录,然后将它们作为派生表连接,以查找您要查找的记录。这看起来像这样:
SELECT t.*, n.[Numéro attribué], m.[Date maintenance], m.Détails
FROM (
Turbopompes AS t
INNER JOIN (
SELECT n1.Turbopompe, n1.[Date attribution], n1.[Numéro attribué]
FROM [Turbopompes Numéros] n1
INNER JOIN (SELECT Turbopompe, MAX([Date attribution]) AS MaxDateAttribution
FROM [Turbopompes Numéros]
GROUP BY Turbopompe) n_max
ON n1.[Date attribution] = n_max.MaxDateAttribution
AND n1.Turbopompe = n_max.Turbopompe
) AS n ON t.[Numéro interne] = n.Turbopompe
) INNER JOIN (
SELECT m1.Turbopompe, m1.[Date maintenance], m1.[Détails]
FROM [Turbopompes Maintenances] m1
INNER JOIN (SELECT Turbopompe, MAX([Date maintenance]) AS MaxDateMaintenance
FROM [Turbopompes Maintenances]
GROUP BY Turbopompe ) m_max
ON m1.Turbopompe = m_max.Turbopompe
AND m1.[Date maintenance] = m_max.MaxDateMaintenance
) AS m ON t.[Numéro interne] = m.Turbopompe;
但是,由于必须通过[日期维护] / [日期归因]来记录记录以查找每个表中的最新记录,无法编写任何返回正确结果的查询可更新的记录集。