Jump to content

Recommended Posts

depending on the situation, it can sometimes be acceptable to just join the data with a character (like a comma) then split on the data to read it back. instead of join, you can also use serialize()/unserialize() to turn an array into a string and back. this way uses more characters, but is safer

I agree with rhodesa.

 

However, the most extensible solution uses three tables.

 

table items

    id int(11) primary key auto_increment

    name varchar(32)

 

table users

    id int(11) primary key auto_increment

 

table users_items

    id int(11) primary key auto_increment

    item_id int(11)

    user_id int(11)

 

And then to get a user's choices:

SELECT
    items.*
FROM
    items 
    LEFT JOIN users_items
    ON users_items.item_id = items.id
WHERE
    users_items.user_id = 828

 

This would get the items associated for user id = 828

I agree with rhodesa.

 

However, the most extensible solution uses three tables.

 

table items

    id int(11) primary key auto_increment

    name varchar(32)

 

table users

    id int(11) primary key auto_increment

 

table users_items

    id int(11) primary key auto_increment

    item_id int(11)

    user_id int(11)

 

And then to get a user's choices:

SELECT
    items.*
FROM
    items 
    LEFT JOIN users_items
    ON users_items.item_id = items.id
WHERE
    users_items.user_id = 828

 

This would get the items associated for user id = 828

 

I love 3NF databases!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.