jeudi 25 février 2016

Laravel groupby ignore when does not match the condition

I want to group by type, but should ignore when there's any entry with the status 1.

I tried to do it with IF, but failed. After a lot of search, I didn't find anything like that, so I decided to ask your help.


Table Example:

|  id   -  status   -   type   |
|   1   -    0      -     1    |
|   2   -    0      -     1    |
|   3   -    1      -     1    | - Should ignore all group.
|   4   -    0      -     2    |
|   5   -    0      -     2    |
|   6   -    0      -     2    |
|   7   -    0      -     3    |
|   8   -    0      -     3    |
|   9   -    0      -     3    |
|   9   -    0      -     3    |
|   10  -    1      -     4    | - Should ignore all group.
|   11  -    0      -     4    |
|   12  -    0      -     4    |
|   13  -    1      -     4    | - Should ignore all group.


Code:

$models = Model::find()
    ->select('type', 'count(*)')
    ->groupBy('type');


What I get:

|  type   -  count(*) |
|   1     -     3     |
|   2     -     3     |
|   3     -     4     |
|   4     -     4     |


What I want: (type 1 and 4 should be ignored, because have entries with status 1)

|  type   -  count(*) |
|   2     -     3     |
|   3     -     4     |



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire