G
G
Grigory Shchetintsev2016-10-13 13:55:25
MySQL
Grigory Shchetintsev, 2016-10-13 13:55:25

How to get max value field from Mysql row?

There is a user_counters table like

ID | count_1 | count_2
---------------------------
1  |    2    |    3
2  |    6    |    3

So, it is not possible to compose a query so that, as a result of the selection, it produces only one of the count_1 or count_2 fields , depending on which one is larger.
For example with a condition WHERE id=1
or WHERE id=2
a simple select query as an example:
SELECT cu.`count_1` `c1`,  cu.`count_2` `c2` FROM `user_counters` `cu` WHERE cu.`id` = 1

How to rebuild it correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-10-13
@Narakot

SELECT IF(cu.`count_1` > cu.`count_2`, cu.`count_1`, cu.`count_2`) as maxcount FROM `user_counters` `cu` WHERE cu.`id` = 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question