Suppose I have a table like the following:
Table name: trial
ID | Date | Name | Status | Type
1 | 2017-06-01 | ABC | Not Active | Food
2 | 2017-06-02 | DEF | Not Active | Food
3 | 2017-06-03 | GHI | Active | Food
To retrieve the last data with type = FOOD from the table above, I use this query
SELECT * FROM `trial` WHERE type = 'FOOD' order by id DESC limit 1
if I want to retrieve the latest data from type = FOOD, but if there the data with status = active, this data is taken.
Assuming the table conditions like this.
ID | Date | Name | Status | Type
1 | 2017-06-01 | ABC | Active | Food
2 | 2017-06-02 | DEF | Not Active | Food
3 | 2017-06-03 | GHI | Not Active | Food
I Use query like this
SELECT * FROM `trial` WHERE type = 'FOOD' AND status = 'ACTIVE' order by id DESC limit 1
But what if the table conditions change with this assumption.
ID | Date | Name | Status | Type
1 | 2017-06-01 | ABC | Not Active | Food
2 | 2017-06-02 | DEF | Not Active | Food
3 | 2017-06-03 | GHI | Not Active | Food
How to retrieve the latest data from type = FOOD, but if there the data type = food with status is active, then use data with status = active, but if there is no active status then the last data use
What sql query should I use ?