ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 23rd, 2005, 09:29 AM
NightVa NightVa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 67 NightVa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 31 m 33 sec
Reputation Power: 5
Coldfusion and SQL with a comma delimeted list in the database

Hi all,

Maybe I'm in the wrong area to make this request but I trust the people in here to know the answer and to reply.

I may have desinged my database incorrectly but as it stands now I have a coaches table. Inside the coaches table I have a column that details the coaches position.

SOme people have more than one position but in order to have only one record per person I use a comma delimited list to state thier positions.

Now I can't figure out what the SQL syntax is to pull a persons name based on only ONE of those choices in the list.

Example: Tfld_coachesInfo_position = Director,Coach

I want to get this person if they are a Director in one curcumstance OR a coach in another.

If this was strictly ColdFusion in a URL I'd simply be able to check something like this by using the CONTAINS keyword.

SQL doesen't seem to have anything like this. I tried IN and LIKE and they either don't return anything OR they return ONLY records that have both.

Hopefully this made sense. Hopefully I'm asking in the right forum.

Nathan

Reply With Quote
  #2  
Old April 23rd, 2005, 10:46 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,682 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 15 h 42 m 54 sec
Reputation Power: 53
Yes, any time you are storing lists is a single database field, that is almost always an indicator that you need to normalize the database. By far the best short- and long-term solution is to create a positions table, and a positions_users table, and then store what users have which positions in the positions_users table.

However, even though it will be slow it is not very maintainable going forward, I don't see why you can't do something like:

Tfld_coachesInfo_position LIKE '%Director%' or Tfld_coachesInfo_position LIKE '%Coach%'

You should also be able to do with with a string function (in Oracle this is the substr() function, it varies from DB to DB).
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian.
How to Post a Question in the Forums

Reply With Quote
  #3  
Old April 24th, 2005, 08:49 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 39 sec
Reputation Power: 1018
most of the time, the comma-delimited lists that i've seen people use are lists of id numbers that implement a one-to-many relationship

the problem with LIKE is that you cannot, for example, just do this --

... where categories like '%3%'

because if the categories value is '1,5,13,17' you will get a false positive

therefore, you end up changing it to

... where categories like '%,3,%'

but this fails to select the row if categories is '3,5,7,15'

eventually you end up doing something like this --

... where concat(',' , categories , ',') like concat(',' , $searchterm , ',')

which works, but will never scale efficiently (always does a table scan)

__________________
r937.com | rudy.ca

Reply With Quote
  #4  
Old April 24th, 2005, 10:39 AM
NightVa NightVa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 67 NightVa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 31 m 33 sec
Reputation Power: 5
Quote:
Originally Posted by r937
most of the time, the comma-delimited lists that i've seen people use are lists of id numbers that implement a one-to-many relationship

the problem with LIKE is that you cannot, for example, just do this --

... where categories like '%3%'

because if the categories value is '1,5,13,17' you will get a false positive


This is exactly whats happening. Would you agree with Kiteless that my problem is lack of normalization of the database? What criteria can I use to determine (in the future) that I need to normalize? I have this one now about lists in a single field. Any others?

Nathan

Reply With Quote
  #5  
Old April 24th, 2005, 12:08 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 39 sec
Reputation Power: 1018
another warning sign is a series of columns named foo1, foo2, foo3, ...

see Fundamentals of Relational Database Design for some normalization guidelines (please disregard that the examples are microsoft access, the techniques work everywhere)

Reply With Quote
  #6  
Old April 24th, 2005, 12:16 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,682 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 15 h 42 m 54 sec
Reputation Power: 53
Quote:
Originally Posted by r937
the problem with LIKE is that you cannot, for example, just do this --

... where categories like '%3%'

because if the categories value is '1,5,13,17' you will get a false positive
Funny, but because I've never actually done this (learned database design very early in my career) I had never even thought of that!

Reply With Quote
  #7  
Old April 25th, 2005, 01:25 PM
Bastion Bastion is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 181 Bastion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 4 h 25 m 11 sec
Reputation Power: 5
Quote:
Originally Posted by r937

eventually you end up doing something like this --

... where concat(',' , categories , ',') like concat(',' , $searchterm , ',')

which works, but will never scale efficiently (always does a table scan)



I've set it up where, on a change to the list field, I add a comma to the end and beginning of the list in the table. So the field would adjust on upload from '3,5,7,15' to ',3,5,7,15,' That way, I can just do the straight LIKE '%,3,%'

Not necessarily the best solution, but since, in my applications (and where I can't adjust the tables to a better system), the person adding the information is in a back-end area it's easier on the front end. Although I could be (and often am) wrong.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > Coldfusion and SQL with a comma delimeted list in the database


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT