Jump to content

Does mutliple selects need a second table?


jeff5656

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.