Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7546693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:15:54+00:00 2026-05-30T09:15:54+00:00

I am using Codeigniter and I am trying to call info from two tables:

  • 0

I am using Codeigniter and I am trying to call info from two tables:

Product:
id,
name,
price,
description,
typeId

Product_Type:
tCategory,
tName

I am trying to pull all info from Product and use Product.typeID to match to the Product_Type table and only pull back the tName. Most of the time there will be at least 3 rows from Product_Type table that will have the same typeID. Example:

Product 1 will be a red shirt for $20 and from type I will need Large, Medium and Small.

I have tried to doing this with JOIN but it gives me the 3 types I need but also duplicate the shirt info 3 times.

Here is my code:

$this->db->select('product.id, product.name, product.price, product.description, product_type.tName');  
$this->db->from('product');
$this->db->where('perm_name', $id);
$this->db->join('product_type', 'product_type.tCategory = product.typeId', 'LEFT OUTER');
$query = $this->db->get(); 

Any help would be greatly appreciated.

EDIT:

Array
(
    stdClass Object
        (
            [id] => 2
            [name] => Tshirt 1
            [price] => 20
            [description] => Awesome tshirt
            [tName] => 1
    )

)

Array
(
    [0] => stdClass Object
        (
            [tCategory] => 1
            [tName] => Small
    )

    [1] => stdClass Object
        (
            [tCategory] => 1
            [tName] => Medium
    )
    [2] => stdClass Object
        (
            [tCategory] => 1
            [tName] => Large
    )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:15:55+00:00Added an answer on May 30, 2026 at 9:15 am

    To have a product row contain a column types which is populated with the rows of a 2nd table, you can either join both tables and play with group by:

    SELECT 
        product.id, 
        max(product.name) as name, 
        max(product.price) as price, 
        max(product.description) as description, 
        group_concat(product_types.tName) as types
    
    FROM
        product
    
    LEFT JOIN
        product_type ON product.tName = product_types.tCategory
    
    GROUP BY product.id
    

    there is no magic behind max(product.name). you are not allowed to use columns in the select-clause that are not in the group by-clause or aggregated. since product.id is the primary key, we will only get one product.name for each product.id, so we don’t care which of the 3 product.name (from the join with the 3 types) gets selected. they are all the same. we could even write any(product.name) in the select-clause but i don’t think mysql supports this. =)

    or do a correlated sub-query ~

    SELECT 
        product.id, 
        product.name, 
        product.price, 
        product.description, 
        (SELECT 
             group_concat(product_types.tName)
         FROM product_types
         WHERE product.tName = product_types.tCategory
         GROUP BY product_types.tCategory
        ) as types
    
    FROM
        product
    

    i suggest to use the first query as it will be easier for mysql to optimize. for the record: i did not test those queries so it’s possible they need some tweaking. just let me know.

    Edit1: Further explanation for using max()

    In the following query we are not allowed to use the column name, because we only grouped by the column id.

    SELECT
        id,
        name /* not allowed */
    
    FROM
        product
    
    GROUP BY id
    

    we may only select columns that are in the group by-clause. we may also use columns, that are not in the group by-clause though aggregate functions like max and group_concat.

    to solve this problem we can just add the column name to the group by-clause

    SELECT
        id,
        name /* ok because it's in group by */
    
    FROM
        product
    
    GROUP BY id, name
    

    if we now have different values for name, we will get more than one tuple in the result, e.g.:

    For the product table (id, name) = {(1, Alice), (1, Bob)} we get the result

    1, Alice
    1, Bob
    

    because we grouped both columns.

    the 2nd approach is using an aggregate function, like max:

    SELECT
        id,
        max(name) /* ok because it's aggregated */
    
    FROM
        product
    
    GROUP BY id
    

    For the product table (id, name) = {(1, Alice), (1, Bob)} we get the result

    1, Bob /* max(Alice,Bob) = Bob, because A < B */
    

    In your example I assumed that the column product.id is the primary key and therefore unique. This means that we can not have different values in the name column for equal values in the id column. {(1, Alice), (1, Bob)} is not possible, but maybe {(1, Alice), (2, Bob)}. If we GROUP BY product.id now, we get a value for product.name for each tuple in the group. But because the id determines the name, those values are all the same:

    SELECT 
        product.id, 
        product.name, 
        product_type.tName,
    
    FROM
        product
    
    LEFT JOIN
        product_type ON product.tName = product_types.tCategory
    

    will result in

    (1, "White T-Shirt", Small),
    (1, "White T-Shirt", Medium),
    (1, "White T-Shirt", Large),
    (2, "Black T-Shirt", Small),
    (2, "Black T-Shirt", Medium),
    (2, "Black T-Shirt", Large)
    

    after grouping it by product.id the result will look like

    (1, F("White T-Shirt", "White T-Shirt", "White T-Shirt"), 
         G(Small, Medium, Large)),
    (2, F("Black T-Shirt", "Black T-Shirt", "Black T-Shirt"), 
         G(Small, Medium, Large))
    

    where F and G are the aggregate functions used in the select-clause. for F it does not matter which value we use, they are all the same. so we just used max. for g we used group_concat to concat all values together.

    therefore

    F("White T-Shirt", "White T-Shirt", "White T-Shirt") = "White T-Shirt"
    F("Black T-Shirt", "Black T-Shirt", "Black T-Shirt") = "Black T-Shirt"
    G(Small, Medium, Large) = "Small, Medium, Large"
    

    this will result in

    (1, "White T-Shirt", "Small, Medium, Large"),
    (2, "Black T-Shirt", "Small, Medium, Large")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using codeigniter and I am trying to call 3 different tables and
I am using latest codeigniter and trying to call stored procedure from my model.
I trying to make my first AJAX with JSON call using jQuery and CodeIgniter.
Using CodeIgniter, I am trying to modify the name of the uploaded file to
I am using PHP5 and CodeIgniter and I am trying to implement a single-sign
Using CodeIgniter, I am trying to place several images onto my view page as
I'm trying to achieve: I am using CodeIgniter. I am trying to access http://localhost/mywebsite/uploads/
I'm trying to send an email using codeigniter, and would like the message to
I am trying to route a URL using codeigniter URL routing. I want to
In my LAMP application (using CodeIgniter), I have a call to $this->db->update... that generates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.