更新行,内部加入并获得顶部行

时间:2018-11-29 16:17:12

标签: sql mariadb

使用MariaDB

我正在尝试使用membershipRenewDate表中officiantsDetails的最大值来更新renewDate表中的officiantsRenewals

officiant_id上的内部联接(两个表的列名称相同)

我有类似的东西,但是语法错误。

UPDATE officiantsDetails offd 
SET offd.membershipRenewDate = offr.renewDate
FROM (SELECT TOP (1) renewDate, officiant_id FROM officiantsRenewals ORDER BY renewDate DESC ) as offr
WHERE offd.officiant_id = offr.officiant_id

2 个答案:

答案 0 :(得分:1)

您应该能够为此使用通用表表达式(CTE)。您使用WITH子句定义“子查询”,然后使用INNER JOIN将CTE扩展到要更新的表。看起来像这样:

WITH Top1RenewDate AS 
(
    SELECT TOP (1) 
        renewDate, 
        officiant_id 
    FROM officiantsRenewals 
    ORDER BY renewDate DESC 
)
UPDATE offd
SET offd.membershipRenewDate = offr.renewDate
FROM officiantsDetails offd
INNER JOIN Top1RenewDate offr ON offd.officiants_id = offr.officiants_id

尽管您需要在该系统上的;关键字之前添加一个WITH,但相同的语法也适用于SQL Server。

答案 1 :(得分:0)

您使用的语法不是MariaDB。在MariaDB中,您可以使用{ "parser": "babel-eslint", "env": { "browser": true, "node": true, "es6": true, "jest": true }, "globals": { "React":true }, "plugins": [ "react" ], "extends": "airbnb", "rules": { "max-len": 0, "one-var": 0, "no-console": ["error", { "allow": ["warn", "error"] }], "no-mixed-operators": 0, "no-multiple-empty-lines": ["error", {"max": 2}], "no-prototype-builtins": 0, "class-methods-use-this": 0, "padded-blocks": 0, "no-return-assign": 0, "no-unused-expressions": ["error", { "allowTernary": true }], "camelcase": 0, "no-plusplus": ["error", { "allowForLoopAfterthoughts": true }], "prefer-destructuring": ["off"], "no-tabs": 0, "no-mixed-spaces-and-tabs": "error", "indent": ["error", "tab", { "SwitchCase": 1 }], "new-cap": 0, "spaced-comment": ["error", "always", { "exceptions": ["*"] }], "import/no-named-as-default": 0, "import/no-unresolved": 0, // Linting will not interpret aliases if this rule is turned on "import/no-extraneous-dependencies": 0, "import/extensions": 0, "jsx-a11y/href-no-hash": 0, "jsx-a11y/no-static-element-interactions": 0, "jsx-a11y/no-noninteractive-element-interactions": 0, //Allow us to put event handlers on any HTML element "jsx-a11y/no-noninteractive-tabindex": 0, "react/forbid-prop-types": [2, { "forbid": ["any"] }], "react/jsx-indent": [2, "tab"], "react/jsx-indent-props": ["error", "tab"], "react/jsx-filename-extension": 0, "react/jsx-no-bind": 1, "react/prefer-stateless-function": [1, { "ignorePureComponents": true }], "react/sort-comp": 0 }, "parserOptions": { "ecmaFeatures": { "jsx": true, "modules": true } } }

JOIN

您使用的语法使人联想到SQL Server。