Click to See Complete Forum and Search --> : help with logic involving tables


ixxalnxxi
10-22-2007, 12:22 AM
so i want to make a website that users can favorite articles, and each article would have a unique ad, as would the user.

so i'm wondering, how should the table scheme be involving the articles and favorites?

what i was thinking is i'd have a table of the users, and i'd have multiple columns that are fav1 fav2 fav3, and each row would store the id of an article. but then that would mean there would be a limit to the amount of favorites a user could have.

anyone have a solution?

cridley
10-22-2007, 07:18 AM
have a 3rd table called 'favourites' with two columns in it 'userID' and 'articleID'. This way user '1' can have unlimited favourites:

UserID...............articleID
1........................3
1........................6
1........................7
2........................4
2........................76
2........................3

etc.

then you can get all the favourites by joining the favourites table with the articles table :

SELECT * FROM favourites LEFT JOIN articles ON favourites.articleID = articles.articleID WHERE favourites.userID = 1

(where 1 is the id of the user whose favourites you're looking for)

This method is valid for most 1-to-many relationships.

ixxalnxxi
10-22-2007, 11:41 AM
dang, i completely forgot that i can merge tables like that, thanks for the advice ^_^ ~