Can I do this with just SQL?

Posted by Josh on Stack Overflow See other posts from Stack Overflow or by Josh
Published on 2010-03-22T20:27:40Z Indexed on 2010/03/22 20:31 UTC
Read the original article Hit count: 268

Filed under:
|
|
|

At the moment I have two tables, products and options.

Products contains

  1. id
  2. title
  3. description

Options contains

  1. id
  2. product_id
  3. sku
  4. title

Sample data may be:

Products
id: 1
title: 'test'
description: 'my description'

Options
id: 1
product_id: 1
sku: 1001
title: 'red'

id: 2
product_id: 1
sku: 1002
title: 'blue'

I need to display each item, with each different option. At the moment, I select the rows in products and iterate through them, and for each one select the appropriate rows from options. I then create an array, similar to:

[product_title] = 'test';  
[description]   = 'my description';  
[options][]     = 1, 1001, 'red';  
[options][]     = 2, 1002, 'blue';

Is there a better way to do this with just sql (I'm using codeigniter, and would ideally like to use the Active Record class)?

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql