0

tbl_products:

+------------+--------------+-------------+------------+
| product_id | product_name | category_id |company_name|
+------------------------------------------------------+
| 1          | iPhone       |      3      |  Apple     |
| 2          | galaxy s1    |      3      |  Samsung   |
| 3          | galaxy s2    |      3      |  Samsung   | 
| 4          | tab          |      4      |  Apple     |
+------------------------------------------------------+

From the above table, i want to get the company name according to the category_id. I have used foreach to get the value. I got Samsung 2 times and Apple once. But i want Samsung and Apple both will show only once. Is it possible? i wrote the bellow code in my model.

    public function select_company_by_category_id($category_id) {
    $this->db->select('*');
    $this->db->from('tbl_products');
    $this->db->where('category_id', $category_id);
    $query_result = $this->db->get();
    $result = $query_result->result();
    return $result;
}
1
  • How exactly do you want your $result to look like? I mean, what do you expect to see there? Commented Feb 1, 2016 at 14:26

3 Answers 3

1

use group_by, and select only company_name :

public function select_company_by_category_id($category_id) {
    $this->db->select('company_name');
    $this->db->from('tbl_products');
    $this->db->where('category_id', $category_id);
    $this->db->group_by('company_name');
    $query_result = $this->db->get();
    $result = $query_result->result();
    return $result;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use group_by

$this->db->group_by('company_name');

Comments

0

It should be apparent that you have more than one record for certain category_ids. You may have 0, 1, or numerous records in practice. That being the case, you would need to either a) report "No company name" if no records were found, b) loop through the records that were found and concatenate them, or c) use a fancier query and a group_concat function to tell SQL to get you all the names. If I'm not mistaken, I think the two prior answers here are incorrect. I believe that just using group_by will still just get you one company_name.

MySQL offers a GROUP_CONCAT function you can use in conjunction with a GROUP BY clause but you might be using some other DBMS. I think Postgresql has array_agg or something like that.

Also, if you use $query_result->result() then you will get an array of objects. If you just use $query_result->row() then you should get a single object instead of an array of objects

At any rate, I tried something like this which seems to work.

public function select_company_by_category_id($category_id) {
    $this->db->select("GROUP_CONCAT(company_name) AS names");
    $this->db->from('tbl_products');
    $this->db->where('category_id', $category_id);
    $this->db->group_by('category_id');
    $query_result = $this->db->get();
    $result = $query_result->row();
    return $result
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.