【问题标题】:How to count based on where in mysql? php如何根据mysql中的位置进行计数? php
【发布时间】:2017-02-20 08:45:59
【问题描述】:

我想知道如何使用 codeigniter 进行查询以显示我需要的计数?

EX:提交选择90和56

$this->db->where('atr_id', 90);
$this->db->or_where('atr_id', 56);

列表表

   id     |    item_id    |   atr_id
---------------------------------------
   1      |        1      |    56
   2      |        1      |    90
   3      |        2      |    56
   4      |        2      |    90
   5      |        2      |    92

预期回报如下所示。

    item_id    |  active_attr   |  total_attr  
----------------------------------------------
        1      |        2       |     2        // active_attr (90,56) and total_attr (90 , 56)
        2      |        2       |     3        // active_attr (90,56) and total_attr (90, 56, 92)

active_attr 计算在item_id 中找到的atr_id 的数量(90、56)

并且total_attr 计算atr_id 的总数item_id

这里真的需要帮助谢谢大家...

【问题讨论】:

  • 您能否澄清一下active_attritem_id 2 的结果是2 吗?
  • @DarshanMehta active_attr 分别是 56 和 90
  • 查看我的 CI 更新

标签: php mysql database codeigniter count


【解决方案1】:

我想你想要的是GROUP BY,这个SQL查询可以一次性给你预期的回报。

SELECT a.item_id, COUNT(DISTINCT a.atr_id) as active_attr, 
COUNT(DISTINCT b.atr_id) as total_attr
FROM test2 a
INNER JOIN test2 b ON a.item_id=b.item_id
WHERE a.atr_id=56 OR a.atr_id=90
GROUP BY a.item_id

这是 ci 查询示例。

<?php
    $res = $this->db
              ->select('a.item_id, COUNT(DISTINCT a.atr_id) as active_attr, COUNT(DISTINCT b.atr_id) as total_attr')
              ->join('test2 as b', 'a.item_id=b.item_id')
              ->where('a.atr_id', 56)
              ->or_where('a.atr_id', 90)
              ->group_by('a.item_id')
              ->get('test2 as a')
              ->result();

【讨论】:

  • 我试过了,效果很好!谢谢! :) 只是好奇谁使用@thomas-g 的性能更好
【解决方案2】:

你可以通过这个查询实现你想要的

SELECT item_id, 
       SUM(
           CASE 
                    WHEN atr_id = 90 THEN 1
                    WHEN atr_id = 56 THEN 1
                    ELSE 0              
                END
            ) AS active_attr, 
            COUNT(*) AS total_atr
FROM List_table
GROUP BY item_id

所以您不需要 WHERE 子句,您的 active_attr 的计数是在 SELECT 中使用 CASE 语句完成的。 CASE 将为您提交的值(56 和 90)返回 1,为其余值返回 0,然后您将所有 1 相加得到计数。

【讨论】:

  • 有什么办法可以采用 codeigniter 格式吗?我在 sql 行中尝试过,它可以工作:)
  • 很抱歉,我对codeigniter一无所知。 SQL FTW :)
【解决方案3】:

您是否按方法应用分组:

 $this->db->group_by('item_id'); 
 $this->db->order_by('item_id', 'desc');

这将产生类似的结果

|  ITEM_ID |  TOTAL  |
|    12    |    3    |
|    15    |    2    |

【讨论】:

    【解决方案4】:

    您正在寻找结果的方面(或分类)。

    我会使用您的第一个查询,通过循环读取结果集并通过一次扫描填充两个方面(两个地图)。

    $item_facet = array();
    $atr_facet = array();
    
    foreach($query->result() as $row){
        $item_id = $row->item_id;
        $atr_id = $row->atr_id;
        $item_facet = increment($item_facet, $item_id);
        $atr_facet = increment($atr_facet, $atr_id);
    }
    
    function increment($map, $id){
        if(isset($map[$id])){
            $map[$id] = $map[$id] + 1;
        }else{
            $map[$id] = 1;
        }
        return $map;
    }
    

    或者这些是两个GROUP BY SQL 查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      相关资源
      最近更新 更多