简体   繁体   English

使用INNER JOIN进行Oracle UPDATE

[英]Oracle UPDATE with INNER JOIN

I need to update the values of a table column from the MAX value of another table column. 我需要从另一个表列的MAX值更新表列的值。

This question is exactly my case where I have a table I need to get the MAX value from and then update the column called datetime of table newTable with the MAX datetime value of that table for all matching home values in newTable. 这个问题正是我的情况,我需要一个表来获取MAX值,然后使用该表的MAX datetime值更新表newTable的datetime列,以获取newTable中所有匹配的home值。

Based on the data from the linked thread, I have come up with the following update statement 根据链接线程的数据,我提出了以下更新声明

UPDATE newTable s
SET s.datetime = (
    SELECT tt.timedate
    FROM topten tt
    INNER JOIN
        (SELECT home, MAX(datetime) AS MaxDateTime
        FROM topten
        GROUP BY home) groupedtt 
    ON tt.home = groupedtt.home 
    AND tt.datetime = groupedtt.MaxDateTime WHERE s.home = tt.home);

The problem with this is that I get the following error 这个问题是我得到以下错误

SQL Error: ORA-01427: single-row subquery returns more than one row

I should also point out that topten.home is not unique while newTable.home is. 我还应该指出topten.home并不是唯一的,而newTable.home是。

I can get rid of the error by adding a rownum statement like so: 我可以通过添加如下的rownum语句来消除错误:

UPDATE newTable s
SET s.datetime = (
    SELECT tt.timedate
    FROM topten tt
    INNER JOIN
        (SELECT home, MAX(datetime) AS MaxDateTime
        FROM topten
        GROUP BY home) groupedtt 
    ON tt.home = groupedtt.home 
    AND tt.datetime = groupedtt.MaxDateTime WHERE s.home = tt.home AND rownum <= 1);

or setting the subquery for MAX 或者为MAX设置子查询

UPDATE newTable s
SET s.datetime = (
    SELECT MAX(tt.timedate)
    FROM topten tt
    INNER JOIN
        (SELECT home, MAX(datetime) AS MaxDateTime
        FROM topten
        GROUP BY home) groupedtt 
    ON tt.home = groupedtt.home 
    AND tt.datetime = groupedtt.MaxDateTime WHERE s.home = tt.home);

however I don't quite understand why this is needed since the MAX statement in the original subquery should make sure there is only 1 entry per home nor do I know what the impact of those changes are exactly (Though initial tests suggest they seem to work) 但是我不太明白为什么需要这样做,因为原始子查询中的MAX语句应该确保每个家庭只有1个条目,我也不知道这些变化的影响究竟是什么(虽然初步测试表明它们似乎有效)

Am I over complicating it? 我复杂了吗?

Why not simply... 为什么不简单......

UPDATE newTable s
SET s.datetime = (
    SELECT COALESCE(MAX(tt.timedate), <put your default date here>)
    FROM topten tt
    WHERE s.home = tt.home)

If I take your original statement, and I remove the inner join, like this: 如果我接受你的原始陈述,我删除了内连接,如下所示:

UPDATE newTable s
SET s.datetime = (
    SELECT tt.timedate
    FROM topten tt
    WHERE s.home = tt.home);

... you will see that subquery can return multiple rows for the same home value. ...您将看到子查询可以为同一个home值返回多行。 So let's say that the above returns 5 rows per home value, and then you add your inner join with the MAX and GROUP BY query which does return a single row per home , it will still return a total of 5 x 1 rows. 因此,假设上面的每个home值返回5行,然后使用MAXGROUP BY查询添加内部联接,该查询确实每个home返回一行,它仍将返回总共5 x 1行。 It won't magically reduce the number of rows to 1 . 它不会神奇地将行数减少到1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM