代码之家  ›  专栏  ›  技术社区  ›  ashish

如何通过在同一列上运行max()来更新列?

  •  2
  • ashish  · 技术社区  · 7 年前

    temp_number        tempdate
    A12345              null
    A12345001          '2018-01-01'
    A12345002          '2018-01-02'
    

    我想把日期定在 A12345 2018-01-02 使用此查询:

    update table_a1 set tempdate = (select max(tempdate) from table_a1 where 
    substr(temp_number,1,6) = 'A12345')
    where temp_number = 'A12345'
    

    上面的查询不起作用,我想使用 max() 函数,而不是给出任何实际值。

    2 回复  |  直到 7 年前
        1
  •  0
  •   ScaisEdge    7 年前

    可以对子查询使用联接以获得最大值

    update table_a1 m
    INNER JOIN (
        select max(tempdate)  max_date, substr(temp_number,1,6) temp1
        from table_a1 
        where substr(temp_number,1,6) = 'A12345')
        group by temp
    ) t on t.temp1 =  m.tempdate 
    set tempdate = t.max_date  
    
        2
  •  1
  •   Tim Biegeleisen    7 年前

    你的 WHERE NULL 记录:

    UPDATE table_a1
    SET tempdate = (SELECT MAX(tempdate)
                    FROM table_a1
                    WHERE temp_number LIKE 'A12345%')
    WHERE temp_number = 'A1234' AND tempdate IS NULL;