Tuesday, 10 September 2013

How to Prioritize OR condition in mysql

How to Prioritize OR condition in mysql

I have table structure something like this
+---+----------+-----------+--------------+
| id| customer | Address | Address_type |
+---+----------+-----------+--------------+
|1 | 1 | Address 1 | 2 |
|2 | 2 | Address 2 | 2 |
|3 | 1 | Address 3 | 1 |
+---+----------+-----------+--------------+
There are two Address_types in Database. I have to select Address based on
following conditions
If Address for costumer of Address_type = 1 is present then display that
address.
If Address_type = 1 is not present and Address_type = 2 is present then
display Address_type = 2 Address for that customer.
If both are present for that customer then display only Address where
Address_type = 1
I have tried this by OR condition but it displays record whichever is
first in Database so there is way in mysql query to achieve this with only
one query? i.e. something like giving priority in OR condition to fetch
only Address_type = 1 record when both Address_types(1 and 2) are present
in Database?

No comments:

Post a Comment