Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 7th, 2003, 11:22 AM
Stevieboy Stevieboy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: NW England
Posts: 1 Stevieboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Stevieboy
Best way to relate products

Hi Chaps

I'm trying to set up a database to be used in an online shop. I want to record a set of related items to a product so that when the customer finds a product they want to buy, we can suggest what else they might need with it or what might complement their purchase.

Does anyone know the best [simplest] way of modelling this so that you can both add a productid to a list of productids that are related and query the database and get out all the related items for a productid. It's difficult to add an intermediate table to one table instead of two.

The way I was starting to do it got big very quickly - I did a table with [id,] productid and relatedproductid and for records 1,2 and 3
I had 1:2, 1:3, 2:1, 2:3, 3:1, 3:2 as 6 different records

Any better ways out there?

Thanks in advance for your help
StevieBoy

Reply With Quote
  #2  
Old February 24th, 2003, 10:36 AM
Baby-Luck's Avatar
Baby-Luck Baby-Luck is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 Baby-Luck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 30 sec
Reputation Power: 0
just a guess ..

Not sure this'll be much help as I'm not speaking from experience but not sure there's another way to do it really.

I guess if the direction in which they were related is not important you could just use 1:2, 1:3, 2:3 and read the connection both ways but it's probably not a good idea.

Don't really see why you need the [id] field, I assume productid and relatedproductid and the same and unique.

Perhaps some normalization sites can help ...

Reply With Quote
  #3  
Old February 25th, 2003, 03:28 PM
RayHogan RayHogan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: NJ, USA
Posts: 11 RayHogan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,

You might try storing a string with each record that contains a comma delimited list of other item ID's. Then, when your user wants to see other related items, your code will take this text field and use it within an "in" clause such as select * from items where ID in ( other_items) order by ...

where other items is a pointer to the current records list of associsted times

Excuse my syntax as I wish to give you the idea.


You will have to do some string programming in order to allow editing this list of items, and you will have to handle the none and the one case and the comma, as well as making sure that the current item can not go into its own list, but this is a fairly easy, non-relational solution to the problem.

Also, I've had difficulty with long "in" lists so watch out for that.


Ray

Reply With Quote
  #4  
Old February 25th, 2003, 04:19 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,310 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 4 h 19 m 26 sec
Reputation Power: 888
this is a classic many-to-many relationship

besides the products table, you need another one

create table relatedproducts
( id1 integer not null
, id2 integer not null
, primary key (id1, id2 )
, foreign key (id1) references products (id)
, foreign key (id2) references products (id)
)

assume there is no "direction" in the relationship, i.e. if product A is related to B then B is also related to A

then to store a relationship, just add a row to that table using both ids

always put the lower of the two in id1

to find products that are related to product 123, the query is
Code:
select id2 as productid
     , productname
  from relatedproducts
inner
  join products
    on id2 = product.id
 where id1 = 123
union all
select id1 as productid
     , productname
  from relatedproducts
inner
  join products
    on id1 = product.id
 where id2 = 123
order by 1


rudy

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Best way to relate products


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway