Mosin Ali
Mosin Ali

Reputation: 29

Count number of entries made by id in a table in Yii2?

enter image description here

I have a table like this now what i want to do is count the records for each ID where STATUS is COMPLETED . Something like SELECT COUNT(*) FROM TABLE WHERE ID= foreach(ID) AND STATUS=1

Upvotes: 0

Views: 205

Answers (3)

Bekir Bekirov
Bekir Bekirov

Reputation: 1

$any_var = Your_Table::find()->where(['status' => 'Cimpleted'])->select('id')->orderBy('id')->groupBy('id')->count ();

Upvotes: -1

Serghei Leonenco
Serghei Leonenco

Reputation: 3507

You can do it using Active Record as this:

I assuming you have you table model named like Job, then you can get a needed value as this:

$number = Job::find()->where(['STATUS' => 'Completed'])->count();

or it is always better to store constant properties in the model like this:

class Job extends ActiveRecord {
         const STATUS_COMPLETED = 'Completed';

then your ActiveQuery will look like this:

$number = Job::find()->where(['STATUS' => Job::STATUS_COMPLETED])->count();

Also here is the full description: Querying Data

Upvotes: 0

aatwork
aatwork

Reputation: 2270

select id, count(id) as count from table where status='COMPLETED' group by id;

Basically you need to use group by clause in MySQL.

Upvotes: 2

Related Questions