我有一个包含以下列的销售表
- 姓名
- 类型(学生、成人、工人)
- 销售额(A、B、C、D)
- 成本
- 位置 (1,2,3,4)
我想按名称获取与名称和组相关的类型、销售额和位置的计数,以便我可以将其显示在表格上
是否可以使用查询获取所有分组值>
任何帮助将不胜感激..
我有一个包含以下列的销售表
我想按名称获取与名称和组相关的类型、销售额和位置的计数,以便我可以将其显示在表格上
是否可以使用查询获取所有分组值>
任何帮助将不胜感激..
使用GROUP BY
然后SUM(condition)
获取您的计数:
SELECT name AS Name,
SUM(type = 'worker' ) AS Worker,
SUM(type = 'student') AS Student,
SUM(type = 'adult' ) AS Adult,
SUM(sales = 'A' ) AS A,
SUM(sales = 'B' ) AS B,
SUM(sales = 'C' ) AS C,
SUM(sales = 'D' ) AS D,
SUM(location = 1 ) AS Location1,
SUM(location = 2 ) AS Location2,
SUM(location = 3 ) AS Location3,
SUM(location = 4 ) AS Location4
FROM sales
GROUP BY Name
function getDistributorStat() // One Distributor
{
$this ->db->select("count(*) ch1 ,
sum(c_privi_flag = 'Y') ch2, sum(c_happy1_flag = 'Y') ch3, sum(c_happy2_flag = 'Y') ch4,
sum(date(D_JOIN) = CURRENT_DATE) nch1,
sum(date(d_privi_activated) = CURRENT_DATE) nch2,
sum(date(d_happy1_activated) = CURRENT_DATE) nch3,
sum(date(d_happy2_activated) = CURRENT_DATE) nch4"
);
$this->db->from('bc_master');
$query = $this ->db->get()->row();
$data['distributors']=$query->ch1;
$data['privilaged']=$query->ch2;
$data['happy1']=$query->ch3;
$data['happy2']=$query->ch4;
$data['new_distributors']=$query->nch1;
$data['new_privilaged']=$query->nch1;
$data['new_happy1']=$query->nch1;
$data['new_happy2']=$query->nch1;
return $data;
}