Reputation: 29
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
Reputation: 1
$any_var = Your_Table::find()->where(['status' => 'Cimpleted'])->select('id')->orderBy('id')->groupBy('id')->count ();
Upvotes: -1
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
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