mercredi 5 août 2015

How To Create Query With Conditional Group By


I have table like this :

+---------+---------------+
| item_id | status        |
+---------+---------------+
| 1       | active        |
| 1       | sold          |
| 1       | deleted       |
| 2       | active        |
| 2       | sold          |
| 3       | active        |
+---------+---------------+

what I want is simple output like this. But, with one condition. if some item_id already have 'deleted' status, then it wont show again in this query. and all item_id are grouped into single line.

+---------+---------------+
| item_id | status        |
+---------+---------------+
| 2       | active        |
| 3       | active        |
+---------+---------------+

I tried using : SELECT * FROM table GROUP BY item_id, but the result is not as I expected.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire