【问题标题】:Query to get limited rows of each type查询以获取每种类型的有限行
【发布时间】:2014-03-07 10:04:33
【问题描述】:

大家好,我正在使用 CI 框架,我已经写了一个查询...如下

public function home_lastest_company()
    {
        $this->db->select('c.id as company_id,c.name as company,c.type,cp.company_logo,it.title as industry,jc.title as category');
        $this->db->from(JB_COMPANY_TABLE . ' c');
        $this->db->join(JB_COMPANY_PROFILE_TABLE . ' cp', 'c.id=cp.jb_company_id', 'left');
        $this->db->join(JB_INDUSTRY_TYPE . ' it', 'c.industry_id = it.id', 'LEFT');
        $this->db->join(JB_CATEGORY . ' jc', 'c.category_id = jc.id', 'LEFT');
        $this->db->where('c.visibility', '1');
        $this->db->where('c.status', '1');
        $this->db->order_by('c.created_date', 'desc');
        $objquery = $this->db->get();
        return $objquery->result_array();
    }

对此的 sql 查询是

 SELECT `c`.`id` as company_id, `c`.`name` as company, `c`.`type`, `cp`.`company_logo`, `it`.`title` as industry, `jc`.`title` as category FROM (`jb_company` c)
    LEFT JOIN `jb_company_profile` cp ON `c`.`id`=`cp`.`jb_company_id` 
    LEFT JOIN `jb_industry_type` it ON `c`.`industry_id` = `it`.`id`
    LEFT JOIN `jb_category` jc ON `c`.`category_id` = `jc`.`id`
    WHERE `c`.`visibility` = '1' AND `c`.`status` = '1' ORDER BY `c`.`created_date` desc

我得到一个这样的数组

Array
(
    [0] => Array
        (
            [company_id] => 14
            [company] => Tech Hive
            [type] => 0
            [company_logo] => clogo8.png
            [industry] => BROADCASTING
            [category] => Electronics
        )

    [1] => Array
        (
            [company_id] => 13
            [company] => WadeTech
            [type] => 0
            [company_logo] => download.jpg
            [industry] => INFORMATION TECHNOLOGY
            [category] => Information Technology
        )

    [2] => Array
        (
            [company_id] => 16
            [company] => Reliance
            [type] => 0
            [company_logo] => reliance.jpg
            [industry] => ELECTRONICS
            [category] => Electronics
        )

    [3] => Array
        (
            [company_id] => 12
            [company] => AVIVA
            [type] => 1
            [company_logo] => aviva.jpg
            [industry] => CONSULTING
            [category] => Management
        )

    [4] => Array
        (
            [company_id] => 11
            [company] => Apple
            [type] => 2
            [company_logo] => apple10.jpg
            [industry] => Agriculture
            [category] => Information Technology
        )

    [5] => Array
        (
            [company_id] => 9
            [company] => Samsung
            [type] => 1
            [company_logo] => samsung.jpg
            [industry] => ELECTRONICS
            [category] => Information Technology
        )
 )

实际上,除了上面显示的数据之外,还有更多的数据....现在我想要的是一个查询,在该查询中,我根据字段类型限制了数据。例如我想要 20 行类型 0 20 行类型 1, 20 行类型 2

【问题讨论】:

    标签: mysql sql codeigniter


    【解决方案1】:

    这在 MySQL 中是可以做到的,但并不像添加 LIMIT 子句那么简单。这里有一篇文章详细解释了这个问题:

    http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

    这是一篇好文章——他为“每组前 N 个”问题介绍了一个优雅但幼稚的解决方案,然后逐渐对其进行改进。

    【讨论】:

      【解决方案2】:

      你可以使用这个:

      step1:使用GROUP BY 命令选择所有组的类型。 例如

      SELECT type FROM TABLE_NAME GROUP BY type
      

      所以现在你得到了一个表中存在的所有类型值。

      第 2 步:然后使用 LIMIT 条件循环命令,输入类型。

      SELECT `c`.`id` as company_id, `c`.`name` as company, `c`.`type`, `cp`.`company_logo`, `it`.`title` as industry, `jc`.`title` as category FROM (`jb_company` c)
          LEFT JOIN `jb_company_profile` cp ON `c`.`id`=`cp`.`jb_company_id` AND type=$array[type] 
          LEFT JOIN `jb_industry_type` it ON `c`.`industry_id` = `it`.`id`
          LEFT JOIN `jb_category` jc ON `c`.`category_id` = `jc`.`id`
          WHERE `c`.`visibility` = '1' AND `c`.`status` = '1' ORDER BY `c`.`created_date` desc LIMIT 0,20
      

      如果您想在您的网站中对概念进行分页,最好尝试这篇文章中以前的答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-02
        • 1970-01-01
        • 2013-05-01
        • 2023-03-07
        • 2021-08-10
        • 1970-01-01
        • 2017-12-03
        • 1970-01-01
        相关资源
        最近更新 更多