How to get min/max of two integers in Postgres/SQL?

后端 未结 2 730
眼角桃花
眼角桃花 2020-12-13 22:48

How do I find the maximum (or minimum) of two integers in Postgres/SQL? One of the integers is not a column value.

I will give an example scenario:

I would l

相关标签:
2条回答
  • 2020-12-13 23:36

    Have a look at GREATEST and LEAST.

    UPDATE my_table
    SET my_column = GREATEST(my_column - 10, 0);
    
    0 讨论(0)
  • 2020-12-13 23:40

    You want the inline sql case:

    set my_column = case when my_column - 10 > 0 then my_column - 10 else 0 end
    

    max() is an aggregate function and gets the maximum of a row of a result set.

    Edit: oops, didn't know about greatest and least in postgres. Use that instead.

    0 讨论(0)
提交回复
热议问题