query against comma delimited field

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default query against comma delimited field

    I can't seem to find out how to do this correctly. I have a field with comma
    delimited
    text that I need to query against to find a match.

    Scenario: A school logs in and would like to see names of volunteers for their
    school only.
    The Names of volunteers come from their web app (Vol table) where they also
    indicate
    certain school(s) (VolMore table) they would like to volunteer at. Normally a
    volunteer will only select
    one preferred school but we do have some cases where they select more than one
    school
    from a list box on their web app. If they do select more than One then that
    info is inserted into
    a db field as comma delimited text. I am trying to query that comma delimited
    text for the school id
    which is identified in a session once the school logs in.

    I think I'm pretty close but can't figure the proper setup for the Where
    clause.

    ANY assistance will be greatly appreciated. Thanks!



    <!--- Query to get Preferred School --->
    <cfquery name="qGetSchool" datasource="dsn">
    SELECT Vol.FName, Vol.LName, VolMore.PrefSchool
    FROM Vol, VolMore
    WHERE VolMore.PrefSchool = '#GetUser.SchID#'
    </cfquery>



    <!--- Loop through Preferrred School listing & output --->
    <cfset list_of_schools = ListToArray(#qGetSchool.PrefSchool#)>


    <cfloop index = "LoopCount"
    from = "1"
    to = #ArrayLen(list_of_schools)#>


    <!--- Query to get Volunteers --->
    <cfquery name="qGetVol" datasource="dsn">
    SELECT Vol.FName, Vol.LName, VolMore.PrefSchool
    FROM Vol, VolMore
    WHERE VolMore.PrefSchool = '#GetUser.SchID#'
    </cfquery>


    <cfoutput>
    #qGetVol.LName#, #qGetVol.FName
    </cfoutput>
    </cfloop>

    cfquest Guest

  2. Similar Questions and Discussions

    1. Reading a Comma Delimited File
      Hello all, So as the title says, I need to read a comma delimited file generated by MS Excel. I know that by doing GetListAt() or ListLen()...
    2. comma delimited list problem
      I have a comma delimited list that is loaded into flash using loadvars. How can I convert it into an array? thanks in advance. Shaun
    3. Splitting Comma delimited list
      Hello everyone, I use the following to split a comma delimited list and get varying results and I don't understand why. my count of the...
    4. pulling stuff from comma-delimited field
      I have a page where I allow the user to select one or more names of employees to send an email to, which is sent automatically when the form is...
    5. generate CSV or comma delimited
      What's the easiest way to generate CSV or a comma delimited file from an ASP recordset? I've seen a few searching the internet and they appear to be...
  3. #2

    Default Re: query against comma delimited field

    Hi cfquest,

    I have a field with comma delimited text...

    There's your trouble. This is not a correctly-designed database structure, so
    there's no "correct" way to do it and you're SOL as far as SQL syntax is
    concerned. The alternative, and a bad one at that, is to write a whole bunch of
    CF to spin through the comma-delimited field, treat it as a list and use CF
    list functions (listfind, listgetat, etc.) to find the values you're looking
    for.

    Actually there's one other alternative: redesign this DB in proper relational
    format.
    For the volunteers' VolMore info, you need:
    Schools Table
    Volunteers Table
    Volunteers/Schools Table

    You can then have the database do the work of getting the right records. It
    would be wicked fast and the data work would be done where it belongs: on the
    data server.

    HTH,

    philh Guest

  4. #3

    Default Re: query against comma delimited field

    Actually there's one other alternative: redesign this DB

    I thought this db was in "proper format." I do have a School Table (the query
    that logs in the school & assigns the session) and the Volunteer Table (Vol &
    VolMore) that retrieves the PrefSchool list. The only field that I can use to
    tie these tables (School/Volunteer) together is by the PrefSchool field since
    Volunteers aren't assigned per say to a particular school. This page will act
    as a "call list" of volunteers wanting to help at their school.

    Thanks for your suggestions.

    cfquest Guest

  5. #4

    Default Re: query against comma delimited field

    So you have a Schools table, with an ID field. And you have a Volunteers table
    with an ID field. What's the point of the VolMore table? is it just a 1-1
    relationship with the Vol table?

    If so, and all that's in there is their preferred school, change the VolMore
    table to be a mapping table between Volunteers and their preferred Schools.
    Name it something like VolSchoolPrefs or something. All it has is 2 columns:
    volunteer ID and school ID. That way it replaces your comma-delimited list
    field and follows relational database design principles.

    If there's more information than just the preferred school list in the VolMore
    table, add a new table. If there's a 1-1 mapping between Vol and VolMore
    though, I question the reasoning and necessity behind separating them into 2
    tables.

    There's really no _nice_ way to handle what you've done (put a comma-delimited
    list of IDs that should be foreign key references to a different table). SQL
    doesn't provide methods to handle it because you aren't supposed to do it. You
    could theoretically do it in coldfusion, but you'd need to basically pull all
    your volunteers from the database, then loop over them, putting the prefschool
    field into a ListContains call for their school ID. Not very lightweight at all.

    Kronin555 Guest

  6. #5

    Default Re: query against comma delimited field

    I agree with philh. If I'm understanding you correctly, you have the right
    number of tables (3), but the structure of your "VolMore" table is wrong.
    Instead of storing a comma delimited list of schools (ex. "18,2,38") in the
    "PrefSchool" column, you want enter each school ID as a separate record like
    this (see example 1)

    With this structure it would be extremely simple to retrieve a list of
    volunteers for a particular school (see example 2)





    Example 1:

    Table [VolunteerSchools]
    ID VolunteerID SchoolID
    ==================================
    1 22 1 (Jack Bell / ABC School)
    2 22 3 (Jack Bell / EFG School)
    3 24 1 (Mary Smith / ABC School)

    *Note: The ID columns should have a numeric data type (not text).

    Example 2:

    --- SQL is not tested
    SELECT v.FirstName, v.LastName
    FROM Volunteers v, VolunterSchools vs
    WHERE vs.SchoolID = #CurrentSchoolID# AND
    vs.VolunteerID = v.VolunteerID

    mxstu Guest

  7. #6

    Default Re: query against comma delimited field

    Excellent example. Now I see what you two are saying. I figured I was hoping
    for a simple solution like "using some magic code" to only loop through just
    the comma delimited field and not all the thousands of records.

    Thanks

    cfquest Guest

  8. #7

    Default Re: query against comma delimited field

    You're welcome. It may take a short time to re-design your table now, but as
    philh pointed out it is a much better structure, your queries will be faster
    and you won't have to worry about this list issue again. In the long term it
    will save you many headaches.

    If you have any problems or questions about how to insert the data, feel free
    to post back and someone will help you with it.

    mxstu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139