我有一个项目,他们有3个状态。现在,我需要计算每天每种状态的数量和总数。
我可以单独列出它们。但是不知道如何将它们结合起来。
这是我的代码:
$passed_counts = (new \yii\db\Query())
->select(['date', 'week', 'count(sku) as sku'])
->from('qa3d')
->where(['status' => 'Passed'])
->groupBy('date')->all();
$failed_counts = (new \yii\db\Query())
->select(['date', 'week', 'count(sku) as sku'])
->from('qa3d')
->where(['status' => 'Failed'])
->groupBy('date')->all();
$onhold_counts = (new \yii\db\Query())
->select(['date', 'week', 'count(sku) as sku'])
->from('qa3d')
->where(['status' => 'On Hold'])
->groupBy('date')->all();
$total_counts = (new \yii\db\Query())
->select(['date', 'week', 'count(sku) as sku'])
->from('qa3d')
->groupBy('date')->all();
现在,我可以在4个表中分别列出它们。我想像这样将它们组合在一张桌子上,但不知道如何。
date Passed Failed On Hold Total
2019-01-16 1 4 2 7
2019-01-17 1 0 2 3
请帮助我。
谢谢。
答案 0 :(得分:3)
尝试一下:
$data = (new \yii\db\Query())
->select([
'date',
'SUM( IF(status = "Passed", 1, 0) ) AS passed',
'SUM( IF(status = "Failed", 1, 0) ) AS failed',
'SUM( IF(status = "On Hold", 1, 0) ) AS onhold',
'COUNT(*) AS total'
])
->from('qa3d')
->groupBy('date')
->all();
答案 1 :(得分:0)
我的解决方法是:
$images = ProductImages::find()
->where(['pi.product_id'=>$id])
->select([
'pi.*',
'f.count as firstCount',
's.count as secondCount',
])
->from([
'pi' => ProductImages::tableName(),
])
->leftJoin(['f'=>First::find()
->select('object_id, COUNT(*) as count')
->where(['type'=>120])
->groupBy(['object_id'])
], 'f.object_id = pi.id')
->leftJoin(['s'=>Second::find()
->select('image_id, COUNT(*) as count')
->where([
'type'=>101,
'is_personal'=>false,
])
->groupBy(['image_id'])
], 's.image_id = pi.id')
->asArray()
->all();