user1630140
user1630140

Reputation: 27

PHP-Update a column with the minimum value of multiple columns

I have a MYSQL Table with multiple columns:

col1 | col2 | col3 | col4 | col5 | col6 | min_all_col |
 25  |  27  |  35  |  21  |  37  |  34  |             |

I need to know how I can update the column (min_all_cols), with the minimum value of all 6 columns e.g 21 I have tried several UPDATE statements but just seem to confuse myself.

Can anyone help me? Regards

Upvotes: 1

Views: 63

Answers (1)

Filipe Silva
Filipe Silva

Reputation: 21657

Use LEAST:

LEAST(value1,value2,...)

With two or more arguments, returns the smallest (minimum-valued) argument.

UPDATE tableName
SET min_all_col = LEAST(col1,col2,col3,col4,col5,col6);

sqlfiddle demo

Upvotes: 1

Related Questions