使用基于另一个表的条件更新一组列?

发布于 2024-09-10 07:45:38 字数 808 浏览 4 评论 0原文

我有两张桌子:

table_1 - ControlID、代码、 报告日期、归档日期年龄、 年龄类别等,

table_2 - ControlID、代码、 报告日期、归档日期等,

table_1 中的 ControlID外键,而不在 table_2 中。我需要用 table_2 中的 ReportedDate 更新 table_1 中的 ReportedDate,并且 Age 和 AgeCatogory 已计算且正常。

我想更新 table_1 中的这三列,其中 ControlID、FiledDate 和 Code 都是相同的。

到目前为止,我已经:

UPDATE table_1 SET ReportedDate=table_2.ReportedDate, Age='<value>' AgeCategory='<value>'
         WHERE table_1.ControlID=table_2.ControlID AND
         table_1.FiledDate=table_2.FiledDate AND table_1.Code=table_2.Code
  

如果有人知道如何解决它???

如有任何帮助,我们将不胜感激...

编辑:

我收到错误消息,指出 MySQL Syntax error at 'FROM ...'

I've two tables :

table_1 - ControlID, Code,
ReportedDate, FiledDate Age,
AgeCategory, etc.,

table_2 - ControlID, Code,
ReportedDate, FiledDate etc.,

ControlID in table_1 is Foreign key whereas not in table_2. I need to update ReportedDate in table_1 with ReportedDate in table_2 and Age and AgeCatogory has been calculated and fine.

I want to update those three columns in table_1, where ControlID, FiledDate and Code in both are identical.

Now far I've :

UPDATE table_1 SET ReportedDate=table_2.ReportedDate, Age='<value>' AgeCategory='<value>'
         WHERE table_1.ControlID=table_2.ControlID AND
         table_1.FiledDate=table_2.FiledDate AND table_1.Code=table_2.Code
  

If anyone has the idea of how could it be resolved???

Anyhelp would be appreciated...

EDIT:

I'm getting error saying MySQL Syntax error at 'FROM ...'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

衣神在巴黎 2024-09-17 07:45:38
    UPDATE table_1
    JOIN table_2 
    ON table_1.ControlID=table_2.ControlID
         AND table_1.FiledDate=table_2.FiledDate
         AND table_1.Code=table_2.Code
    SET table_1.ReportedDate=table_2.ReportedDate, 
         table_1.Age='<value>',
         table_1.AgeCategory='<value>';
    UPDATE table_1
    JOIN table_2 
    ON table_1.ControlID=table_2.ControlID
         AND table_1.FiledDate=table_2.FiledDate
         AND table_1.Code=table_2.Code
    SET table_1.ReportedDate=table_2.ReportedDate, 
         table_1.Age='<value>',
         table_1.AgeCategory='<value>';
℉絮湮 2024-09-17 07:45:38

UPDATE 语法中不允许使用 FROM 1

UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
    SET col_name1=expr1 [, col_name2=expr2 ...]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

如果如果您想从第二个表中获取内容,您可能需要使用子查询。

试试这个代码:

UPDATE table_1 SET ReportedDate=
    (SELECT ReportedDate FROM table_2
      WHERE table_1.ControlID = table_2.ControlID
      AND table_1.Code = table_2.Code
    ), Age='<value>' AgeCategory='<value>'

There is no FROM allowed within the UPDATE syntax 1:

UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
    SET col_name1=expr1 [, col_name2=expr2 ...]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

If you like to fetch content from a second table, you might want to use a subquery.

Try this code:

UPDATE table_1 SET ReportedDate=
    (SELECT ReportedDate FROM table_2
      WHERE table_1.ControlID = table_2.ControlID
      AND table_1.Code = table_2.Code
    ), Age='<value>' AgeCategory='<value>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文