Deidara
Deidara

Reputation: 677

MySQL select the minimum value from the maximum values

I want to select the minimum of the max values returned from all the tables. Here's my sql command for searching all the max values:

SELECT MAX(value) FROM temperatureTable 
UNION 
SELECT MAX(value) FROM resistanceTable 
UNION 
SELECT MAX(value) FROM pressureTable;

Here's the result:

    {
        "MAX(value)": "8.113130E-1"
    },
    {
        "MAX(value)": "6.445700E+0"
    },
    {
        "MAX(value)": "6.526210E-8"
    }

How do I select the minimum value 6.445700E+0 with sql command?

Also, how do I access the MAX(value)/MIN(value) from the return Rawdatapackage?

result[0].Max(value) // will generate an error because of the '()'.

Upvotes: 2

Views: 250

Answers (2)

Renato Tarso
Renato Tarso

Reputation: 76

Use subquery technique:

SELECT MIN(tmp.value) 
FROM  
(   SELECT MAX(value) AS value 
    FROM temperatureTable 
UNION    
    SELECT MAX(value) AS value 
    FROM resistanceTable
UNION
    SELECT MAX(value) AS value 
    FROM pressureTable
) AS tmp

Upvotes: 2

Alex Tartan
Alex Tartan

Reputation: 6836

You can use

SELECT MAX(value) as maxValue FROM temperatureTable 
UNION 
SELECT MAX(value) FROM resistanceTable 
UNION 
SELECT MAX(value) FROM pressureTable;

and access results like:

result[0].maxValue  
// using "as someAlias" names that field result "someAlias"

If you want the minimum max value, you can:

Select min(maxValue) as minValue FROM (
    SELECT MAX(value) as maxValue FROM temperatureTable 
    UNION 
    SELECT MAX(value) FROM resistanceTable 
    UNION 
    SELECT MAX(value) FROM pressureTable;
) x

I'm not sure it makes any sense to do this, since those tables hold different types of measurements.

Upvotes: 1

Related Questions