Derby db:sql更新select语句的结果

时间:2012-12-07 11:49:39

标签: sql sql-update derby

我有两张桌子:

元素:

ID (int. key)
TYPE (String)

ELEMENT_ATTRIBUTES:

ELEMENT_ID (int)
ATTR_NAME (String)

(我忽略了原始表格中对于这个问题不重要的字段)

我想做点什么:

UPDATE ELEMENT e, ELEMENT_ATTRIBUTES a
SET a.ATTR_NAME='new name'
WHERE e.ID = a.ELEMENT_ID
  AND e.TYPE = 'a specific type'
  AND a.ATTR_NAME = 'old name'

这可能是在derby sql中吗?

1 个答案:

答案 0 :(得分:4)

查看the Derby Reference Manual on the UPDATE statement的语法是:

{
    UPDATE table-Name [[AS] correlation-Name]
        SET column-Name = Value
        [ , column-Name = Value} ]*
        [WHERE clause] |
    UPDATE table-Name
        SET column-Name = Value
        [ , column-Name = Value ]*
        WHERE CURRENT OF
}

所以不,你不能像你的问题那样执行查询。你需要修改它有点像:

UPDATE ELEMENT_ATTRIBUTES a
SET a.ATTR_NAME='new name'
WHERE a.ATTR_NAME = 'old name'
AND EXISTS (SELECT 1 
            FROM ELEMENT e 
            WHERE a.ELEMENT_ID = e.ID AND e.TYPE = 'a specific type')