如何在Postgres / SQL中获得两个整数的最小值/最大值? [英] How to get min/max of two integers in Postgres/SQL?

查看:1183
本文介绍了如何在Postgres / SQL中获得两个整数的最小值/最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Postgres / SQL中找到两个整数的最大值(或最小值)?整数之一不是列值。

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

我举一个例子:

我想从一列(所有行)中减去一个整数,但结果不应小于零。因此,首先,我有:

I would like to subtract an integer from a column (in all rows), but the result should not be less than zero. So, to begin with, I have:

UPDATE my_table
SET my_column = my_column - 10;

但这可能会使某些值变为负数。我想要的(用伪代码)是:

But this can make some of the values negative. What I would like (in pseudo code) is:

UPDATE my_table
SET my_column = MAXIMUM(my_column - 10, 0);


推荐答案

看看 GREESTST和LEAST

UPDATE my_table
SET my_column = GREATEST(my_column - 10, 0);

这篇关于如何在Postgres / SQL中获得两个整数的最小值/最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆