简体   繁体   中英

Find largest and smallest value across numerous columns on specific row?

I have a table in MySQL named 'partshistory' as follows:

Part        *col1*      *col2*      *col3*
------------------------------------------
AAA1000     1.10        1.15        1.05
AAB1000     1.85        0.50        2.30
AAC1000     20.00       19.95       10.10

The column names besides 'Part' are unknown, as these are labelled with the date when the data is added. But this could be on any date.

But given a part number, say 'AAB1000', how can I go about finding both the largest, and smallest values across all the rows for that part?

So the output would be:

0.50 and 2.30.

Many thanks in advance!

Combine MIN() and MAX() with LEAST() and GREATEST() .

For example:

select
  min(least(col1, col2, col3)) as smallest,
  max(greatest(col1, col2, col3)) as biggest
from partshistory
where part = 'AAB1000'

Since the table name is partshistory I assumed there could be multiple rows for the same part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM