Sathvik Chinnu Sathvik Chinnu - 3 months ago 20
MySQL Question

Best way to get required row MYSQL

I would like to write a query that checks the particular record and if it is not found it should return the other result

For example:

SNO | FIRST_COLUMN | SECOND_COLUMN | STATUS
1 | value1 | value2 | 1
2 | value1 | value2 | 1
3 | value1 | value2 | 0
4 | value1 | value2 | 1


In the above example the query should return the row with
status
value
0
. If the status with value
0
doesn't exists then it should return any row with
status
value
1
.

Please accept my apologize if my explanation was bad.

Thanks in advance.

Answer

Use order by and limit:

select   SNO, FIRST_COLUMN, SECOND_COLUMN, STATUS
from     mytable
order by STATUS
limit    1