Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 02-16-2005, 11:33 AM   PM User | #1
Taylor_1978
Regular Coder

 
Join Date: Jun 2003
Location: Australia
Posts: 506
Thanks: 6
Thanked 7 Times in 7 Posts
Taylor_1978 is on a distinguished road
Adding more than one value to a field and spliting upon retrieval

Hiya,

I have seen this done before, but not quite sure exactly how to implement it.

In a HTML Form, I have the users choose at least one age group. The values are 1,2,3,4,5,6 and 7. The user selects from a multiple select box so they can choose more than one option.

I want these multiple options stored in the one field, so it may look something like this: 1, 2, 7.

I need to know how I actually go about this and then how I seperate/split it when retrieving. For example: If I do a search:

mysql_query("SELECT * FROM teams WHERE age=1")

That the rows that have the value of say:

1. 1,2
2. 1,5,6
3. 5,7
4. 1

Rows 1 2 and 4 will be retrieved because they have 1 in them.

Any suggestions would be greatly appreciated.

Thanks in advance!

Taylor.
Taylor_1978 is offline   Reply With Quote
Old 02-16-2005, 11:41 AM   PM User | #2
Kiwi
Regular Coder

 
Join Date: Oct 2002
Posts: 379
Thanks: 0
Thanked 0 Times in 0 Posts
Kiwi is an unknown quantity at this point
You would be much better to set-up a child record that would allow you to add as many selections as your like. This would be a table, keyed by the parent ID (eg personID) and the selection. For each option a person selects, you create a new child record.
__________________
Strategy Conscious
Kiwi is offline   Reply With Quote
Old 02-16-2005, 11:52 AM   PM User | #3
Taylor_1978
Regular Coder

 
Join Date: Jun 2003
Location: Australia
Posts: 506
Thanks: 6
Thanked 7 Times in 7 Posts
Taylor_1978 is on a distinguished road
Well - yes if only it were that simple! But I'm never simple! LOL

See... there already is a parent/child record.

Basically what I have is a table called teams. teams has 8 fields in it.

id, userid, ageid, regionid, sportid, genderid, sectionid, levelid

Each of these are number values and each field, except 'id' has a parent table.

These are: ages, regions, sports, genders, sections, levels.

SO... A person creates a new team which has the age groups Under 12 and Under 14 (values are 1 and 2), and they play Basketball and Baseball (values 2 and 3).. If someone then browsing does a search for a Basketball Team that plays Under 12s it needs to find "WHERE sportid=2 AND ageid=1" - HOWEVER if they are looking for a team that plays BOTH Basketball and Baseball in Under 12s, I don't want the search coming up with 2 results - when it is the same team. The result should just be Team #1, plays Basketball and Baseball in Under 12... as opposed to two diff results of: Team #1, plays Basketball in Under 12, Team #2, plays Baseball in Under 12.

Did that make any sense at all?
Taylor_1978 is offline   Reply With Quote
Old 02-17-2005, 12:14 AM   PM User | #4
Kiwi
Regular Coder

 
Join Date: Oct 2002
Posts: 379
Thanks: 0
Thanked 0 Times in 0 Posts
Kiwi is an unknown quantity at this point
Remember, we live in the 00s: parents can have as many children as they like and children can have more than one parent.

One of the first rules of normalisation is to create atomistic data - each field represents only one piece of information. Breaking this rule tends to make the design very messy.

The sort of selects you're talking about will be significantly easier with a normalised design (not to mention more efficient and effective - what you've proposed will mean both 'LIKE' clauses all over the place and a risk of losing data integrity). Add in good use of 'DISTINCT' clauses, the 'CONCAT' function and maybe even a sub-select or two, and you'll be able to get the results you need without sacrificing good design principles.

There are ways to do exactly what you're asking, but it is not a good idea.
__________________
Strategy Conscious
Kiwi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:57 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.