MySQL在两个(或多个)给定值中选择最小值/最大值 [英] MySQL Select minimum/maximum among two (or more) given values

查看:1769
本文介绍了MySQL在两个(或多个)给定值中选择最小值/最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在两个或多个值中将SELECT设置为最小值或最大值.我需要这样的东西:

Is it possible to SELECT the minimum or maximum among two or more values. I'd need something like this:

SELECT MAX_VALUE(A.date0, B.date0) AS date0, MIN_VALUE(A.date1, B.date1) AS date1
FROM A, B
WHERE B.x = A.x

我可以仅通过使用MySQL来实现吗?

Can I achieve this by only using MySQL?

推荐答案

您可以使用LEASTGREATEST函数来实现它.

You can use LEAST and GREATEST function to achieve it.

SELECT
    GREATEST(A.date0, B.date0) AS date0,
    LEAST(A.date1, B.date1) AS date1
FROM A, B
WHERE B.x = A.x

两者均在此处进行了描述 http://dev.mysql.com/doc/refman /5.0/en/comparison-operators.html

Both are described here http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html

这篇关于MySQL在两个(或多个)给定值中选择最小值/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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