简体   繁体   中英

How to find the max of each row in a table in MYSQL

I have this table below which has N rows. here i simplify it to 3 rows.

Example Table

        A                   B                   C                      D
5.640700669769904   5.623475843268976   5.644739934022418
5.62489798487818    5.581388826442135   5.62376476667017
5.62708139972593    5.606610562903928   5.592366625377977

i need to find the new column D which consists of the maximum of the row for columns A,B,C.

in MYSQL MAX(X) only applies to the column. How would i solve this problem?

I have tried with sub tables but no luck.

max() is an aggregate functions that operates over values that are store across rows (although some databases, namely SQLite, allows using it for list of values).

You can use greatest() to get the greatest value over the three columns on each row:

select t.*, greatest(a, b, c) d from mytable

On the other hand, if you want the greatest value over all 3 columns and all rows , you can use both max() and greatest() together:

select max(greatest(a, b, c)) overall_greatest from mytable

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