计算每个项目的共享数量=1(mysql、magento)

计算每个项目的共享数量=1(mysql、magento),mysql,magento2,Mysql,Magento2,我想计算有多少具有相同“item_id”的项的值为“is_shared”=1 示例1。对于项目_id=8517,共享_计数的值应为0,因为有0个项目_id=8517的值为is_shared=1 示例2。对于项目_id=8519,共享_计数的值应为1,因为有一个项目的项目_id=8519,且共享值设置为1 Mysql查询: 'MIN(`main_table`.`id`) AS id', 'main_table.item_id',

我想计算有多少具有相同“item_id”的项的值为“is_shared”=1

示例1。对于项目_id=8517,共享_计数的值应为0,因为有0个项目_id=8517的值为is_shared=1

示例2。对于项目_id=8519,共享_计数的值应为1,因为有一个项目的项目_id=8519,且共享值设置为1

Mysql查询:

                'MIN(`main_table`.`id`) AS id',
                'main_table.item_id',
                'main_table.course_id',
                'main_table.code',
                'main_table.student_id',
                'main_table.product_data',
                'main_table.is_shared',
                'main_table.recipient_email',
                'MAX(`main_table`.`is_shareable`) AS is_shareable',
                'MAX(`main_table`.`grace_period_end`) AS grace_period_end',
                'MAX(`main_table`.`expired_at`) AS expired_at',
                'COUNT(`main_table`.`student_id`) AS student_count',
                '(select count(`main_table2`.`student_id`) from iways_enrollment_item as main_table2 where `main_table2`.`item_id`=`main_table`.`item_id` and `main_table2`.type_id=\'bundle\') as student_count_bundle',
                'MAX(`sales_order_item`.`qty_invoiced` - `sales_order_item`.`qty_canceled`) as qty_paid',
                '(SELECT IF(COUNT(`iei`.`student_id`), 1, 0) ' .
                'FROM `iways_enrollment_item` `iei` WHERE `iei`.`student_id` = ' . $this->currentCustomer->getCustomerId() . ' AND `iei`.`course_id` = `main_table`.`course_id` ' .
                'AND (`iei`.`expired_at` >= CURDATE() OR `iei`.`expired_at` IS NULL)) AS current_user_enrolled',
                '(SELECT IF(COUNT(`soi`.`parent_item_id`), 1, 0) FROM `sales_order_item` `soi` WHERE `soi`.`parent_item_id` = `sales_order_item`.`item_id`) AS is_parent',

您可以尝试对is_shared=1的boolena结果求和

select  item_id, sum(is_shared=1)
from my_table  
group by  item_id 
或为过滤结果计数

select  item_id, count(*)
from my_table  
where is_shared = 1
group by  item_id 

请以文本而不是图像形式共享数据。