Bigbohne
Bigbohne

Reputation:

How can I get maximum and minimum values in a single query?

I'm using MySQL, and I'm looking for a way to get the global maximum and minimum values (for the whole table) from two columns (for example, posx and posy), using only one query.

Upvotes: 7

Views: 16967

Answers (4)

alexn
alexn

Reputation: 58962

SELECT
    MIN(colx) AS minimum,
    MAX(colx) AS maximum,
    MIN(coly) AS minimum,
    MAX(coly) AS maximum
FROM table

Upvotes: 4

chaos
chaos

Reputation: 124297

It's really no more complex than it sounds.

SELECT MIN(posx), MAX(posx), MIN(posy), MAX(posy)
FROM yourtable

Upvotes: 4

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67147

Simple:

SELECT MIN(posx), MIN(posy), MAX(posx), MAX(posy) FROM table

Upvotes: 18

Tom Ritter
Tom Ritter

Reputation: 101330

 select max(posx), min(posy) from table

Upvotes: 2

Related Questions