我需要从两个领域获得最大的价值:
SELECT MAX(field1), MAX(field2)
现在,我如何从这两个中获得最大的价值?
您可能需要使用以下GREATEST()功能:
GREATEST()
SELECT GREATEST(field1, field2);
如果要从所有行中获取绝对最大值,则可能需要使用以下内容:
SELECT GREATEST(MAX(field1), MAX(field2));
范例1:
SELECT GREATEST(1, 2); +----------------+ | GREATEST(1, 2) | +----------------+ | 2 | +----------------+ 1 row in set (0.00 sec)
范例2:
CREATE TABLE a (a int, b int); INSERT INTO a VALUES (1, 1); INSERT INTO a VALUES (2, 1); INSERT INTO a VALUES (3, 1); INSERT INTO a VALUES (1, 2); INSERT INTO a VALUES (1, 4); SELECT GREATEST(MAX(a), MAX(b)) FROM a; +--------------------------+ | GREATEST(MAX(a), MAX(b)) | +--------------------------+ | 4 | +--------------------------+ 1 row in set (0.02 sec)