I have a table with the following structure
id(int) | col1(int) | col2(int) | col3(int) ------------------------------------------ 1 | NULL | 10 | 20 2 | 5 | NULL | 30 3 | 8 | NULL | NULL
Given an ‘id’ value I need to find the minimum non-null value across all the columns in that record.
Like for example, for id=1, the value is 10. For id =2 the value is 5 and so on.
How do I do this in MySQL?
LEAST() would be best if it was NULL safe, but since it isn’t, this is the cleanest solution i can think of:
if you need it in one line, this is the best i can think of: