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

  1. #1

    Default Finding Records

    I have a table with 7 columns (keyword1, keyword 2, etc) - what I need to do is
    to find matching rows based on up to seven input fields, but not less than
    three.

    Thus, if my user types in seven input fields, I need it to return all records
    that match three or more of the input fields. Caveat: input field 3 may return
    under column 5, input field 6 may be under column 1, etc, so the input field
    can match anyone of the seven columns.
    Thus if my user is looking for a series of keywords, it must return all
    records that have three or more of these keywords.

    I've written SQL queries longer than "war and peace", but its not 100%
    effective and very inelegant. Can anyone suggest a more elegant and easy way
    to write this query?

    Using CFMX7 and MS Access (I know, I know...)


    Jellyhead Guest

  2. Similar Questions and Discussions

    1. Finding the PR of a URL
      Hi there, I am looking to find a way and wirte a custom tag that gets the Google PR of a URL. In other words, input is a URL and output is an...
    2. finding a file by name
      code in test.cfm <cfdirectory directory='#GetDirectoryFromPath(GetTemplatePath())#' name='myDirectory' sort='name ASC'> <cftable ...
    3. Help finding records with the earliest time
      I have a database of telephone records that are attached to 20 associates who made phone calls. I need to create a report that shows averages,...
    4. OT: Finding a job
      Hi group, Looking for a job. I'm at the point where I will go to a place and say, "Look, I'll give you $500 or half of my first week's salary,...
    5. Finding a job
      Ray, Sounds like your in a tough position... I know that you just (or about to) have a new place built for you so you don't want to move. ...
  3. #2

    Default Re: Finding Records

    Redesign your db structure. You should not have those columns in your table.

    You should have another table, which would contain an id pointing to the
    record it is associated with and a keyword column
    eg
    Table - myKeyWords
    recordId number
    myKeyWord -varchar

    Then your query will be

    Select YourColumn
    From yourTable Inner Join myKeyWords On yourTable.ID = myKeyWord.recordid
    Where 0=0 <!--- zero equals zero --->
    <cfif Len(Trim(form.keyword))>
    And myKeyWord.myKeyWord = '#form.keyword#'
    </cfif>
    <!--- repeat as many times as needed --->
    <cfif Len(Trim(form.keyword1))>
    And myKeyWord.myKeyWord = '#form.keyword1#'
    </cfif>
    Having Count(myKeyWord) >= 3

    Ken

    The ScareCrow Guest

  4. #3

    Default Re: Finding Records

    Thank you, I'm now actually getting somewhere, but I?m still stuck. I see the
    logic, but the syntax escapes me. Lets get more specific.

    Table 1: cities
    rowid - counter
    city - varchar

    (based on ScareCrow's suggestion)

    Contains a list of some 50-odd cities. New York, Johannesburg, Blacktown,
    Seattle Wellington, etc

    Then
    Table 2: clients
    rowid - counter
    client_id - varchar
    city1 - varchar
    city2 - varchar
    ...
    city 7 - varchar

    This has about 450 unique client records, no client is represented twice.
    Now I need to select into 7 input fields 7 cities. The query must return all
    client_ids that went to 3 or more of these specified cities.
    (Don't worry about the spelling and consistency - city list is a genereated
    drop-down box, so we don't need "like" or "trim" functions - I think)

    Please talk to me like I'm a 2-year-old, I've been sitting with this all day!
    Joins are new to me, and I'm battling a bit.

    Jellyhead Guest

  5. #4

    Default Re: Finding Records

    I assume that the "client_id" is the unique id to identify the client, correct ?

    Then

    Table 1: cities
    rowid - counter
    city - varchar

    Table 2: clients
    rowid - counter
    client_id - varchar

    Table3:clients_cities
    client_id - varchar
    rowid - number

    Lets assume that on the page the user selects 7 cities from the dropdown
    select list.
    This dropdown list will be built like

    <select name="select_cities">
    <cfoutput query="qry_cities">
    <option value="#rowid#">#city#</option>
    </cfoutput>
    </select>

    This will result in a list of rowid's being passed to the action page
    containing the query

    The query

    Select client_id
    From clients Inner Join clients_cities On clients.client_id =
    clients_cities.client_id
    Where 0=0
    <cfif Len(Trim(form.select_cities)>
    And clients_cities.city IN (#form.select_cities#)
    </cfif>
    Having Count(client_cities.city) >= 3
    Group By client_id

    This query will join the 2 tables together only where the client_id is found
    in both tables. It will then filter the recordset to only match the records
    where the city column in the clients_cities table is in the list. It will then
    group the resultset on the client_id. Then filter the recordset to only
    include client_id's that have a count of the city column of 3 or more

    Ken


    The ScareCrow 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