查询以获取每种类型的有限行

时间:2014-03-07 10:04:34

标签: mysql sql codeigniter

大家好我使用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
        )
 )

实际上有更多的数据,然后一个显示在上面....现在我想要的是一个查询,其中我根据类型feild获得数据限制。例如,我想要20行0型20行1型,20行2型

2 个答案:

答案 0 :(得分:0)

这可以在MySQL中完成,但它并不像添加LIMIT子句那么简单。这篇文章详细解释了这个问题:

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

这是一篇很好的文章 - 他为“每组前N个”问题引入了一个优雅但天真的解决方案,然后逐渐改进。

答案 1 :(得分:0)

你可以用这个:

步骤1:使用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

如果您想在自己的网站中使用分页概念,请在此帖子中尝试以前的答案。