0

嘿,我在尝试更新数据库中的表时遇到此错误:

每个派生表都必须有自己的别名

这是我试图更新的mysql查询:

 UPDATE (SELECT clientinfo.idNumber, clientinfo.theCompName, clientoffers.idNumber, 
 clientoffers.expTimeWaiting, clientoffers.theDateStart,
 clientoffers.theDateEnd, clientoffers.theOffer, clientoffers.isActive, 
 clientoffers.theType, clientoffers.fixedAmount, clientoffers.fixedUsed
 FROM clientinfo, clientoffers
 WHERE clientoffers.idNumber = clientinfo.idNumber
 AND clientoffers.theDateEnd >= '2010-06-03'
 AND clientoffers.theDateStart <= '2010-06-03'
 AND clientoffers.idNumber = 'NB351560'
 AND clientoffers.isActive = 'YES')
 SET clientoffers.fixedUsed = clientoffers.fixedUsed + 1

这看起来不错,但由于某种原因似乎不起作用?

任何帮助都会很棒!:o)

更新

 sqltemp = "UPDATE clientinfo, clientoffers SET clientoffers.fixedUsed = clientoffers.fixedUsed + 1 WHERE clientoffers.idNumber = clientinfo.idNumber" & _
             "AND clientoffers.theDateEnd >= '2010-06-03'" & _
             "AND clientoffers.theDateStart <= '2010-06-03'" & _
             "AND clientoffers.idNumber = 'NB351560'" & _
             "AND clientoffers.isActive = 'YES'"
        oRecordset.Open sqltemp, oConnection,3,3

它给了我 ODBC 驱动程序不支持请求的属性的错误。

我正在使用带有经典 ASP 的 Mysql 5.1。

大卫

4

2 回答 2

1

为什么那里有选择?这甚至有必要吗?

为什么不:

 UPDATE clientinfo, clientoffers

 SET clientoffers.fixedUsed = clientoffers.fixedUsed + 1

 WHERE clientoffers.idNumber = clientinfo.idNumber
 AND clientoffers.theDateEnd >= '2010-06-03'
 AND clientoffers.theDateStart <= '2010-06-03'
 AND clientoffers.idNumber = 'NB351560'
 AND clientoffers.isActive = 'YES'

或者更进一步,如果两个表上有一个外键约束idNumber(或者你可以假设每个clientoffer条目总是有一个对应的clientinfo条目):

 UPDATE clientoffers

 SET clientoffers.fixedUsed = clientoffers.fixedUsed + 1

 WHERE clientoffers.theDateEnd >= '2010-06-03'
 AND clientoffers.theDateStart <= '2010-06-03'
 AND clientoffers.idNumber = 'NB351560'
 AND clientoffers.isActive = 'YES'
于 2010-06-04T04:03:05.397 回答
0

“客户报价”。在括号外无效,因此您需要为 select 语句创建别名(尝试 UPDATE (SELECT...) foo SET foo.fixedUsed = foo.fixedUsed + 1

但是,一次对单个表进行更新会更简单:

UPDATE clientoffers
SET fixedUsed = fixedUsed + 1
WHERE
EXISTS (SELECT * FROM clientinfo WHERE
clientoffers.idNumber = clientinfo.idNumber)
AND clientoffers.theDatEnd >= '2010-06-03'
AND clientoffers.theDateStart <= '2010-06-03'
AND clientoffers.idNumber = 'NB351560'
AND clientoffers.isActive = 'YES

除了,根据 MySQL 版本,可能不支持 EXISTS 子查询语法。如果您遇到这种情况,您可以:

a.) 升级 MySQL 或切换到另一个数据库引擎 b.) 使用您的代码在循环中运行 select,然后,对于每个返回的行,发出一个更新语句,记住锁定两个表。

于 2010-06-04T03:05:46.297 回答