Solved

Combine 3 tables

Posted on 2011-09-24
8
254 Views
Last Modified: 2012-05-12
I have three tables I'm trying to combine

products which contains:
product_id, name, price
1, test1, 1.00
2, test2, 1.00
3, test3, 1.00

product_images which contains:
product_id, file_path, upload_number
2, testing.jpg, 1
2, testing2.jpg, 2
3, testing4.jpg, 1
3, testing4.jpg, 2
3, testing4.jpg, 3

product_image_order which contains:
product_id, order_number
2, 1
2, 2
3, 3
3, 1
3, 2

basically.. im trying to grab all of products and attach the product_images and product_image_order by matching the product_id together

i have the following code so far.. but not working =(
$this->db->select($this->product_table_name . '.product_token, '
					. $this->product_table_name  . '.status, '
					. $this->product_table_name  . '.price, '
					. ' GROUP_CONCAT(DISTINCT ' .  $this->product_image_table_name . '.base_filename) AS base_filename', false);
$this->db->from($this->product_table_name);
$this->db->join($this->product_image_table_name, $this->product_table_name . '.product_token = ' . $this->product_image_table_name . '.product_token', 'right');
$this->db->where($this->product_table_name . '.seller_id', (int) $seller_id);
//$this->db->limit($limit, $offset);
//$this->db->order_by($sort_by, $sort_order);

Open in new window

0
Comment
Question by:dsrnu
  • 3
  • 2
  • 2
8 Comments
 
LVL 14

Accepted Solution

by:
Scott Madeira earned 334 total points
ID: 36593784
I think you have a flawed table structure...

The link between products and images is good with the product_id field in the images table.  What you also need in the product_images table is an image_id so that you can use that to link to the image order table.  The relationship is between image_order and images, not products.

so, add an image_id field to product_images and have a query like this:


 
select 
	products.name,
	products.price,
	product_images.file_path,
	product_image_order.order number
from
	products
	left join product_images on (products.product_id = product_images.product_id)
	left join product_images_order on (	product_images.image_id = product_image_order.image_id)

order by
	products.name,
	product_image_order.order_number

Open in new window


You may also want to consider combining the last two tables by adding an order column to product_images since the third table doesn't do anything but hold an order number and there is a one to one relationship (I think) between that table and the product_images table.

You will need to check syntax.  I didn't have anything to test with.
0
 
LVL 14

Assisted Solution

by:Scott Madeira
Scott Madeira earned 334 total points
ID: 36593788
One other thing.  in the third table you will want product_image_id and not product_id if you choose to keep it as three tables.
0
 

Author Comment

by:dsrnu
ID: 36593904
smadeira.. thanks.. but for some reason, it breaks apart when i try to use

GROUP_CONCAT(DISTINCT on product_images.file_path.. any ideas?
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 36593908
I recommend that you combine product_images with product_image_order so you can use ORDER BY on a single table.  I think this will make your code simpler.  

This has some links to interesting philosophical readings on the issue of table design.
http://lmgtfy.com?q=Should+I+Normalize+my+Database
0
 

Author Comment

by:dsrnu
ID: 36593934
so far, i get the following (ive removed the order of the images for now)

finding a really difficult time grouping the base_filename together whenever it belongs to the same product_token (or product_id)
Array
(
    [0] => stdClass Object
        (
            [product_token] => 3d6b30e93a9a9416d3ff04dcb8ed3e85
            [status] => active
            [price] => 2.0000
            [base_filename] => 
        )

    [1] => stdClass Object
        (
            [product_token] => 355b6a8da1634097112aa52396f0b736
            [status] => hidden
            [price] => 20.0000
            [base_filename] => 7f876a48780a1205eede59825c75832a
        )

    [2] => stdClass Object
        (
            [product_token] => 71d232ecb4a3a70a1f0999c4ba889f55
            [status] => comingsoon
            [price] => 0.0000
            [base_filename] => 6c2d841c81238ad708f0806efa10af48
        )

    [3] => stdClass Object
        (
            [product_token] => 71d232ecb4a3a70a1f0999c4ba889f55
            [status] => comingsoon
            [price] => 0.0000
            [base_filename] => 7aa445a75f9e9fc4a5e7648b3e6c072d
        )

)

Open in new window

0
 

Author Comment

by:dsrnu
ID: 36593952
the solution was to use group by! thanks both of you for helping!

@ray: yea i noticed the duplication there.. thanks!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36593996
Thanks for the points -- it's an interesting DB design question!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now