I have table:
Rating:
id | one | two | three
1 | 12 | 3 | 7
2 | 11 | 30 | 3
3 | 8 | 14 | 4
How can i get with SQL MAX values from these fields (one, two, three)? For this example this is 30.
The MySQL Solution If you're working with MySQL, you can combine MAX() with the GREATEST() function to get the biggest value from two or more fields.
In the real world, you will often want to select multiple columns. Luckily, SQL makes this really easy. To select multiple columns from a table, simply separate the column names with commas!
Yes, it is possible to use MySQL GROUP BY clause with multiple columns just as we can use MySQL DISTINCT clause.
In MySQL you can use the GREATEST Function:
SELECT MAX(GREATEST(one, two, three))
FROM T;
Example on SQL Fiddle
SELECT MAX(field) FROM (
SELECT one AS field FROM table
UNION
SELECT two AS field FROM table
UNION
SELECT three AS field FROM table
) AS t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With