Yii2 结合多个计数和分组查询
Yii2 combine multiple count and group by query
我有一个项目,他们有 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 table 秒内将它们单独列出。我想像这样将它们组合成一个 table 但不知道如何。
date Passed Failed On Hold Total
2019-01-16 1 4 2 7
2019-01-17 1 0 2 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();
我的解决方案是:
$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();
我有一个项目,他们有 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 table 秒内将它们单独列出。我想像这样将它们组合成一个 table 但不知道如何。
date Passed Failed On Hold Total
2019-01-16 1 4 2 7
2019-01-17 1 0 2 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();
我的解决方案是:
$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();