D
D
Doniyor Mamatkulov2018-02-15 13:15:40
Yii
Doniyor Mamatkulov, 2018-02-15 13:15:40

Array comparison?

Hi all!
There is a table in the database with fields:

id - автоинкремент, 
subscriber_id - число (может повторяться), 
alive - жива ли запись (может принимать 1 или ноль)

I want to compare the total number of each subscriber_id and the total number of zeros for each subscriber_id
Right now the data in the database is:
id   | subscriber_id | alive
33  | 15               | 0
34  | 15               | 0

I make this comparison:
$subSessCount = Sessions::find()->where(['subscriber_id'=>$sub->id])->asArray()->all();
            $subSessDead = Sessions::find()->where(['subscriber_id'=>$sub->id, 'alive'=>0])->asArray()->all();
            if(count($subSessCount) == count($subSessDead)) {
                $i++;
            }

Server response: 4. Why??? There are only two lines with zeros in the database ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander null, 2018-02-15
@doniyorbekm

One query
SELECT COUNT(subscriber_id) as subscriber_count, subscriber_id, SUM(alive)
FROM table
GROUP BY subscriber_id
Count how many alive=1 and subtract from subscriber_count

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question