Help Ordering Records

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Help Ordering Records

    I have a database with two fields: ID and SVCS.
    SVCS contains a comma delimited list.
    So records on the dabase might look like this:
    ID - SVCS
    1 - a,c,e,g
    2 - b,d,f,h
    3 - a,b,c
    4 - f,g,h
    5 - a,b,c,e

    To search this DB a user checks a series of checkboxes on a form, for example:
    SVCS: a[x] b[x] c[x] d[ ] e[x] f[ ] g[ ] h[ ]

    I want to pull any records where any of the items selected by the user
    is found in the SVCS field.

    So I take the resulting list from the form: a,b,c,e
    and create a query by looping through it as follows:

    <cfquery name="SearchResults">
    Select * from my_table where
    <cfset count=0>
    <cfloop index="i" list="#FORM.SVCS#">
    <cfset count=count+1>
    (SVCS LIKE '%#i#%')<cfif count lt listlen(FORM.SVCS)>OR</cfif>
    </cfloop>
    </cfquery>
    NOTE:I could also use the "IN" statement instead of "LIKE" but I don't think
    it would make a diference for my purpouses.

    That returns:

    1 - a,c,e,g (3 matches: a,c,and e)
    2 - b,d,f,h (1 match: b)
    3 - a,b,c (2 matches: a,c)
    5 - a,b,c,e (4 matches: a,b,c,and e)

    THE QUESTION:
    How do I order the results by the number of matches?

    DogBot Guest

  2. Similar Questions and Discussions

    1. Grouping and ordering
      The manual says "MySQL extends the use of GROUP BY so that you can use non-aggregated columns or calculations in the SELECT list that do not appear...
    2. table ordering
      Hi, Hope someone may be able to help me. I have created a dropdown style menu in Fireworks, and a movie in Flash, and am putting it all together...
    3. Ordering by date ???
      I have created a DB in Access 2000. The Date Field (clientdate) is currently a MEMO field. The date is being entered as: 10 June 2004 21...
    4. Ordering of recordsets
      Create some links (or a drop-down <select> list) that the user can click on to select the way they want the collection ordered. Dynamically build...
    5. [PHP] File ordering
      > I am doing a 'readdir' on a subdirectory. I did my file naming counting on Just sort the $imgFiles array. -- Lowell Allen
  3. #2

    Default Re: Help Ordering Records

    Normalize your database and you won't have problems like this.
    Dan Bracuk Guest

  4. #3

    Default Re: Help Ordering Records

    I agree with comment here, normalize your database! You should not have a DB
    field with comma-delimited values. BUT - if you inherited this task, here's a
    plan. Assuming you are using MSSQL, use the routine you have to load matching
    records into a table variable, ex. @holding_table, making sure @holding_table
    has a column you can use for tallying number of matches.

    After your routine loads records to @holding_table, re-run your routine
    against @holding_table and update the tally column for each record when you get
    a match. Then you can, as the last line in your code:

    select * from @holding table order by tally_column desc

    You could also use your routine and CF's QueryNew, QueryAdd, etc. to achieve
    the same results if you don't use MSSQL.

    If you are not familiar with table variables, research them, they are
    extremely useful and quick.



    drforbin1970 Guest

  5. #4

    Default Re: Help Ordering Records

    If I'm understand you correctly, the length of the string in svcs should grow
    with the number of matches. So ORDER BY len (svcs) should order the output
    ascending or descending based on the number of matches.

    SELECT * FROM my_table
    WHERE svcs LIKE '%#i#%'
    OR svcs LIKE '%#i#%'
    ORDER BY len(svcs) desc

    JaredJBlackburn Guest

  6. #5

    Default Re: Help Ordering Records

    Another way is to simply add up the number of matches for each record - either
    in the select clause or in the order by.

    ... ORDER BY
    CASE WHEN var like '%11%' THEN 1 ELSE 0 END +
    CASE WHEN var like '%12%' THEN 1 ELSE 0 END +
    CASE WHEN var like '%13%' THEN 1 ELSE 0 END

    - OR -

    SELECT ...,
    CASE WHEN var like '%11%' THEN 1 ELSE 0 END +
    CASE WHEN var like '%12%' THEN 1 ELSE 0 END +
    CASE WHEN var like '%13%' THEN 1 ELSE 0 END AS theRank
    FROM ...
    WHERE...
    ORDER BY theRank

    Put the logic in the SELECT if you want to return the number of matches with
    the result set.

    JaredJBlackburn 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