How to insert Multiple selected checkbox values in

2019-05-17 08:38发布

问题:

i want to insert multiple selected checkbox value in one colunm in MS Sql server 2005/08 Eg. checkboxes are:

Cricket Football Painting

Table structure like:

    Id | name     | Hobbies
   ----|----------|--------------------------
    1  | Atish    | cricket,football,painting
    2  | Swapnil  | football, painting

can you help me?

回答1:

There is something called the third normal form: use it :)

Basically this means for you, you need to separate the hobbies from that table, create a seperate table with the possible hobbies and create a lookup-table between the people and the hobbies.

The dirty way would be to define a seperator and insert smth like cricktet|football|painting to the column: but I would really not suggest to do that!



回答2:

You can make three tables

[Names]
Name_ID (int)
Name (varchar)

[Hobbies]
Hobby_ID
Hobby (varchar)

[Hobbies_Names]
Name_ID (foreign key)
Hobby_ID (foregin key)

So, if Atish, who has Name_ID = 1, and hobbies cricket,football,painting, which have Hobby_ID: 1, 2 and 3, your Hobbies_Names table would look like this:

    Name_Id | Hobby_ID
   ---------|----------|
    1       | 1
    1       | 2
    1       | 3

Then you could make a SQL query that selects all Hobbies, where Name_ID = 1 for example