【问题标题】:Access 2010: How to make a query result updateable?Access 2010:如何使查询结果可更新?
【发布时间】:2012-05-22 20:16:28
【问题描述】:

我正在处理我的第一个 Access 2010 数据库,但在编辑从查询返回的记录集时遇到了问题。

我有两个表,“drugs”和“warehouse_stock”。在“药物”中,我有药物的名称。在另一张表中,我有相应的信息。对于那些在“药品”表中的药品,药剂师将管理它们的数量和其他相对值,例如每种药品的输入数量和特定的有效期。药剂师可以将相同的药物信息导入“warehouse_stock”,但有效期不同。

“warehouse_stock”中的每一行数据都以active_substance/strength/strength_type/dosage_form为特征。这四个值在“药物”中的一行数据中必须相同。 Pharmacist 将能够更新现有行数据的 curr_date、in_quant、out_quant_expiry_date 和品牌(来自“warehouse_stock”)并导入“drugs”中存在的新数量的药物。

我现在的查询是:

SELECT warehouse.active_subs,
   warehouse.strength,
   warehouse.strength_type,
   warehouse.dosage_form,
   warehouse.curr_date,
   warehouse.in_quant,
   warehouse.out_quant,
   warehouse.expiry_date,
   warehouse.available_stock,
   warehouse.brand,
   warehouse.ID
FROM   drugs
       RIGHT JOIN warehouse
         ON ( drugs.active_substance = warehouse.active_subs )
            AND ( drugs.strength = warehouse.strength )
            AND ( drugs.strength_type = warehouse.strength_type )
            AND ( drugs.dosage_form = warehouse.dosage_form )
WHERE  ( ( ( warehouse.active_subs ) LIKE
"*" &
Forms!Pharmacy.form!pharmacy_warehouse_stock_Subform.form!warehouse_stock_act_sub & "*" )
AND ( ( warehouse.strength ) LIKE
"*" &
Forms!Pharmacy.form!pharmacy_warehouse_stock_Subform.form!warehouse_stock_strength & "*" )
AND ( ( warehouse.strength_type ) LIKE
"*" &
Forms!Pharmacy.form!pharmacy_warehouse_stock_Subform.form!warehouse_stock_strength_type & "*" )
AND ( ( warehouse.dosage_form ) LIKE
"*" &
Forms!Pharmacy.form!pharmacy_warehouse_stock_Subform.form!warehouse_stock_dosage_form & "*" ) );

有没有办法使结果可更新?

提前感谢您的任何建议!

齐诺纳斯

【问题讨论】:

  • @Remou 是的,warehouse.ID 是唯一索引。还有drugs.ID,但我不使用它。
  • 我不知道你为什么会有这些 Like 语句。实力应该等于实力吗?此外,如果您需要四个字段来匹配药物,则应将它们编入索引。最后,为什么不把药号放在仓库表里,省去很多重复和痛苦呢?
  • 如何索引四个字段以匹配药物?
  • 我找到了索引!那么,我如何在我的情况下使用这个索引?对不起,我是新手:-(
  • active_sub 不是唯一的,强度等也不是唯一的。只有它们的组合是唯一的!

标签: sql ms-access select join sql-update


【解决方案1】:

试试:

<...>
warehouse.ID
FROM  warehouse 
LEFT JOIN Drugs
     ON ( drugs.active_substance = warehouse.active_subs )
        AND ( drugs.strength = warehouse.strength )
        AND ( drugs.strength_type = warehouse.strength_type )
        AND ( drugs.dosage_form = warehouse.dosage_form )

【讨论】:

  • 为什么需要加入毒品?您没有使用任何字段。为什么不在仓库表中包含药物id,而不是复制数据?为什么你有这么多 LIKE 语句?
  • like 语句对我很有用,因为我使用搜索引擎从数据库中检索所有相对值。我没有在仓库表中包含药物 ID,因为我想检查四个字段相同。值不是唯一的 id 而是来自四个字段。我使用 join 来实现这一点。
  • 还有其他问题吗?特别是,为什么不使用药物ID,为什么要加入?
  • 四个字段:active_subs、strength、strength_type和dose_form定义了一行数据的唯一性,而不是id。对于加入问题,我编辑了之前的评论。
  • 在这种情况下,如果您拥有的只是非唯一 id 上的索引,那么您就没有唯一索引。您会发现很难创建没有唯一索引的可更新查询。我怀疑您的数据库设计需要工作。考虑为两个表添加唯一索引。
【解决方案2】:

您的要求完全不明确,标题与您对问题的文字描述以及提供的查询有很大不同,甚至不同。

如果像 active_subs、strength、strength_type、dose_form 等字段在两个表中应该具有相同的值,那么你的设计可能有缺陷。您应该从其中一张桌子上摆脱它们。为什么不将这些字段放在一个表中并通过 id 连接它们?

编辑:经过无休止的讨论,这是我的理解。存在很大的沟通差距,所以我相信 OP 对此并不满意 :)

SELECT warehouse.ID, drugs.quantity, warehouse.active_subs, warehouse.strength, 
       warehouse.strength_type, warehouse.dosage_form, warehouse.curr_date, 
       warehouse.in_quant, warehouse.out_quant, warehouse.expiry_date, 
       warehouse.available_stock, warehouse.brand --whatever values you want
FROM   warehouse
JOIN   drugs ON drugs.active_substance = warehouse.active_subs AND 
                drugs.strength = warehouse.strength AND 
                drugs.strength_type = warehouse.strength_type AND
                drugs.dosage_form = warehouse.dosage_form
WHERE  (....); --if ever there is one
UPDATE warehouse_stock SET curr_date=@curr_date, in_quant=@in_quant, 
                           out_quant=@out_quant, expiry_date=@expiry_date, 
                           brand=@brand;

我不清楚 OP 想要更新什么值。在更新查询中使用合适的 where 子句。

【讨论】:

  • 不幸的是,我想做的不是他们!
  • 我想查询药物信息,然后有能力更新一些值。
  • 你想更新到底层数据库吗?如果是这样,请使用更新查询
  • 是的,完全正确。我可以在现有的选择查询中使用更新查询吗?
  • 是的,绝对可以!但这里的麻烦是要求根本不是查询。更新前需要选择哪些值?
【解决方案3】:

为了使该表可更新,您需要删除未在 From 或 Where 子句中使用的连接(直到现在)。但是,您提到您试图仅过滤具有 drugs 值的 warehouse 结果。为此,您应该使用 Exists 函数,它允许您传递相关子查询(因为内部查询中的项目与外部查询中的项目匹配)。

Select W.active_subs, W.strength, W.strength_type, W.dosage_form
    , W.curr_date, W.in_quant, W.out_quant, W.expiry_date
    , W.available_stock, W.brand, W.ID
From warehouse As W
Where  ( ( ( W.active_subs ) LIKE
"*" &
Forms!Pharmacy.form!pharmacy_warehouse_stock_Subform.form!warehouse_stock_act_sub & "*" )
AND ( ( W.strength ) LIKE
"*" &
Forms!Pharmacy.form!pharmacy_warehouse_stock_Subform.form!warehouse_stock_strength & "*" )
AND ( ( W.strength_type ) LIKE
"*" &
Forms!Pharmacy.form!pharmacy_warehouse_stock_Subform.form!warehouse_stock_strength_type & "*" )
AND ( ( W.dosage_form ) LIKE
"*" &
Forms!Pharmacy.form!pharmacy_warehouse_stock_Subform.form!warehouse_stock_dosage_form & "*" ) )
    And Exists  (
                Select 1
                From drugs As D1
                Where D1.active_substance = W.active_subs
                    And D1.strength = W.strength
                    And D1.strength_type = W.strength_type
                    And D1.dosage_form = W.dosage_form
                )

【讨论】:

  • “存在”不起作用。我没有得到这个查询的结果。
  • @zinon - 如果你去掉 Where 子句的其他元素,只剩下 Exists 子句,你会得到结果吗?
猜你喜欢
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
相关资源
最近更新 更多