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

  1. #1

    Default SQL syntax issue

    Hi all,

    I'm using CFMX (v6), by the way.

    I'm try to cobble together a search page that will take the information input
    on a form and search the database using the LIKE operator.

    Regardless of whether the DISTINCT keyword is used, a duplicate of the
    appropriate row is returned (In this case, I'm searching for a fragment unique
    to a single row, such as SEARCH TERM: 'zac' from DATABASE ELEMENT: 'Zack's
    Chicken Hole').

    Could it be because I'm pulling from two tables?



    SELECT DISTINCT tbl_customers.custID, cust_name, cust_addr1, cust_addr2, city,
    state, zip,
    desk_phone, cell_phone
    FROM tbl_customers, tbl_contacts
    WHERE cust_name LIKE '%#form.customer#%'

    crux_online Guest

  2. Similar Questions and Discussions

    1. Query of Queries syntax issue
      I have query 1 which has a list of names and query 2 which has ratings for the names. Every rating from query 2 has a name in query 1, but not...
    2. simple syntax issue?
      Hi, I just want a function to return true, but after a LoadVars.onLoad event. Am I right in assuuming that the return in the onLoad = function...
    3. Hopefully simple syntax issue with forms
      First let me say that I am relatively new to ColdFusion. Hopefully you gurus out there will be able to steer me int he right direction. I have a...
    4. Syntax error -- data type issue?
      Hi, I keep getting the following error when testing a page with form to insert record. System.Data.OleDb.OleDbException: Syntax error in...
    5. PHP Include Syntax Issue
      What is wrong with this syntax? <?php @include($navLH . '.php'); ?> I am getting a parse error.
  3. #2

    Default Re: SQL syntax issue

    Where is your join between tbl_customers and tbl_contacts?
    Dan Bracuk Guest

  4. #3

    Default Re: SQL syntax issue

    There is a join between the two tables, but it exists on CUST_ID (which is not
    mentioned in the WHERE clause...yet). You see, in creating this search, my
    plan was to have successive CFIF tags to prevent a comparison on a NULL value
    (I can't attach the proposed code right now, but can later if needed). The
    form used to gather info from the user contains multiple fields so that if a
    user only know a portion of several of the various fields containing customer
    information, the search will take into account all of the fields filled out and
    not JUST cust_name, for example.

    This may be a little ambitious for me at my level, but I'd love to get this
    running and in production here at the shop.

    Your input is appreciated.

    crux_online Guest

  5. #4

    Default Re: SQL syntax issue

    Unless and until you include the join condition, you'll get "duplicates" of the customer name, because the current query is a Cartesian join.
    philh Guest

  6. #5

    Default Re: SQL syntax issue

    Originally posted by: Dan Bracuk
    If you set up your database relationships properly, you won't get null values
    on cust_id. You won't need to use cfif at all.

    I understand what you're saying to be that if the DB is correctly designed,
    there should be no NULL values in the cust_ID field, and you're right. The
    database is so designed. However, if one were to go to the site, one would be
    greeted by an empty form with fields for entering the customer's name, address,
    city, state, zip, contact name, and so on. On the back end, the DB has a table
    for customers (containing their company name, address, etc.) and a table for
    contacts (containing their personal name, various phone numbers, position,
    department, email, etc.), so a join (in this case on cust_ID) is required in
    order to bring these tables together.

    With this in mind, the user must enter some snippet of information into one or
    more of these empty form fields, but will not be required to fill ALL fields.
    This means that when the user click the "Search" button, the CF code must
    perform a search of the DB based on the available form fields, some of which
    may be NULL (or empty or '', whichever is appropriate). The CFIF tags are
    being used to remove the NULL fields from the WHERE clause. (code attached
    below).

    Originally posted by: philh
    Unless and until you include the join condition, you'll get "duplicates" of
    the customer name, because the current query is a Cartesian join.

    This is a great point that I completely overlooked. Thank you. In trying to
    get the conditions to work in the WHERE clause, I removed my join altogether.
    It's back in there now.

    Below is the complete unedited query. You'll notice most of the CFIFs have
    been commented out temporarily while I adjust, research, and re-adjust.

    In it's present state, the duplicate is removed, but the query returns the
    proper row PLUS one other row that does not match the seach criteria:

    *** RETURNED DATA ***
    2 Records Found.
    Company Name Address
    Zack's Chicken Hole 123 Monkey Way
    Aces, LLC 786 D Industrial Park Dr
    *** END ***

    You can see that with a search term of "zac" in the cust_name field, the first
    entry should be the only row returned, but I'm a bit suspect of the LIKE
    operator. Perhaps it is returning "Aces, LLC" because of the 'ac' that it and
    the search term shares. I didn't think it worked quite like that.

    More likely, is that it is returning all companies that HAVE contacts
    associated with them and the CFIF is using the wrong test, in which case the
    question becomes, "how should the CFIF's be used to test for empty form
    fields?" As an additional note, Zack's and Aces are the only companies in the
    test DB that have contacts, and each has exactly ONE corresponding contact.

    Hopefully this clarifies a few things and raises good questions.



    <cfquery datasource="#DS#"
    name="searchCustomers"
    username="#UN#"
    password="#PW#">

    SELECT tbl_customers.custID,
    cust_name,
    cust_addr1,
    cust_addr2,
    city, state, zip,
    desk_phone,
    cell_phone
    FROM tbl_customers, tbl_contacts
    WHERE tbl_customers.custID = tbl_contacts.custID
    <cfif form.customer EQ ''>AND cust_name LIKE '%#form.customer#%'</cfif>
    <!---
    <cfif form.addr1 EQ ''>AND cust_addr1 LIKE '%#form.addr1#%'</cfif>
    <cfif form.addr2 EQ ''>AND cust_addr2 LIKE '%#form.addr2#%'</cfif>
    <cfif form.city EQ ''>AND city LIKE '%#form.city#%'</cfif>
    <cfif form.state EQ ''>AND state LIKE '%#form.state#%'</cfif>
    <cfif form.zip EQ ''>AND zip LIKE '%#form.zip#%'</cfif>
    <cfif form.phone EQ ''>AND desk_phone LIKE '%#form.phone#%'</cfif>
    <cfif form.phone EQ ''>AND cell_phone LIKE '%#form.phone#%'</cfif>
    --->
    </cfquery>

    crux_online Guest

  7. #6

    Default Re: SQL syntax issue

    Now we're getting somewhere.

    <cfif form.customer EQ ''>AND cust_name LIKE '%#form.customer#%'</cfif>

    You're asking for every record, because every record will match an evaluation
    of LIKE('%%'). Use NEQ instead, because you want to evaluate only fields that
    have user input.

    HTH,



    philh 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