SQL repeating rows Problem

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

  1. #1

    Default SQL repeating rows Problem

    Please can anyone help.

    I have a search where i search for employee by firstname or lastname or both
    columns. It also searches other columns but my problem is around the firstname
    and lastname.
    When i have the Firstname ticked it displays the correct amount of rows.
    When i have the Surname ticked it also displays the correct amout of rows.
    When both are ticked it repeats the results by the amount of sites.
    Now i guess its something to do with the table join and the OR part of the SQL.
    Any help would be appreciated.

    My query is as follows:


    <cfquery datasource="mydsn" name="qry_GetEmployeeData">
    SELECT

    EMPLOYEE_LOOKUP.FIRST_NAME,
    EMPLOYEE_LOOKUP.LAST_NAME,
    EMPLOYEE_LOOKUP.MIDDLE_INITIALS,
    EMPLOYEE_LOOKUP.SITE_LOCATION,
    EMPLOYEE_LOOKUP.JOB_TITLE,
    EMPLOYEE_LOOKUP.FAX_NUMBER,
    EMPLOYEE_LOOKUP.AREA,
    EMPLOYEE_LOOKUP.KO_NUMBER,
    EMPLOYEE_LOOKUP.EXT_NUMBER,
    EMPLOYEE_LOOKUP.DEPARTMENT,
    EMPLOYEE_LOOKUP.WORK_NUMBER,
    EMPLOYEE_LOOKUP.EMAIL,
    EMPLOYEE_LOOKUP.EMPLOY_NUMBER,
    EMPLOYEE_LOOKUP.MOBILE,
    EMPLOYEE_LOOKUP.CAR_REG,
    EMPLOYEE_LOOKUP.COMP_CAR,
    EMPLOYEE_LOOKUP.CAR_REG2,
    EMPLOYEE_LOOKUP.COST_CENTER,
    EMPLOYEE_LOOKUP.CAR_MAKE,
    EMPLOYEE_LOOKUP.ID,
    EMPLOYEE_LOOKUP.COMMENTS,
    EMPLOYEE_LOOKUP.DISPLAY_STATUS,
    EMPLOYEE_LOOKUP.DISPLAY_DATE,
    EMPLOYEE_LOOKUP.SAPID,
    SITES.ADDRESS_LINE1,
    SITES.ADDRESS_LINE2,
    SITES.ADDRESS_LINE3,
    SITES.ADDRESS_LINE4,
    SITES.ADDRESS_LINE5,
    SITES.ADDRESS_LINE6,
    SITES.TELEPHONE,
    SITES.FAX,
    SITES.SHORT_NAME,
    SITES.MAP_LINK,
    SITES.COUNTRY,
    SITES.LOC_PHOTO,
    SITES.DIRECTIONS_LINK


    FROM
    UKCHAPP7.EMPLOYEE_LOOKUP, UKCHAPP7.SITES
    WHERE
    EMPLOYEE_LOOKUP.DISPLAY_STATUS = 'Y'
    AND
    EMPLOYEE_LOOKUP.DISPLAY_DATE <= '#DateFormat(Now())#'

    AND
    EMPLOYEE_LOOKUP.SITE_LOCATION = SITES.SITE_NAME



    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS TICKED SEARCH
    FORENAME AND SURNAME ############### --->
    <cfif isdefined('Form.SurName') AND isdefined('Form.ForeName') >
    <cfif #Form.ForeName# EQ "true" AND #Form.SurName# EQ "true" AND
    #SearchTerm# NEQ "" >
    AND
    EMPLOYEE_LOOKUP.FIRST_NAME LIKE '%#Trim(SearchTerm)#%'
    OR
    EMPLOYEE_LOOKUP.LAST_NAME LIKE '%#Trim(SearchTerm)#%'
    </cfif>
    </cfif>
    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS TICKED SEARCH
    FORENAME AND SURNAME ############### --->



    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS NOT TICKED
    SEARCH ONLY FOR FORENAME ############### --->
    <cfif isdefined('Form.SurName') AND isdefined('Form.ForeName') >
    <cfif #Form.ForeName# EQ "true" AND #Form.SurName# EQ "false" AND
    #SearchTerm# NEQ "" >
    AND
    EMPLOYEE_LOOKUP.FIRST_NAME LIKE '%#Trim(SearchTerm)#%'
    </cfif>
    </cfif>
    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS NOT TICKED
    SEARCH ONLY FOR FORENAME ############### --->



    <!--- ########## IF THE FORENAME IS NOT TICKED AND THE SURNAME IS TICKED
    SEARCH ONLY FOR SURNAME ############### --->
    <cfif isdefined('Form.SurName') AND isdefined('Form.ForeName') >
    <cfif #Form.ForeName# EQ "false" AND #Form.SurName# EQ "true" AND
    #SearchTerm# NEQ "" >
    AND
    EMPLOYEE_LOOKUP.LAST_NAME LIKE '%#Trim(SearchTerm)#%'
    </cfif>
    </cfif>
    <!--- ########## IF THE FORENAME IS NOT TICKED AND THE SURNAME IS TICKED
    SEARCH ONLY FOR SURNAME ############### --->



    <!--- ########## IF THE koNumber IS NOT TICKED SEARCH ONLY FOR KONUMBER
    ############### --->
    <cfif isdefined('Form.KoNumber') AND isdefined('SearchTerm')>
    <cfif #Form.KoNumber# EQ "true" AND #SearchTerm# NEQ "">
    AND
    EMPLOYEE_LOOKUP.KO_NUMBER LIKE '%#LCase(Trim(SearchTerm))#%'

    </cfif>
    </cfif>
    <!--- ########## IF THE koNumber IS NOT TICKED SEARCH ONLY FOR KONUMBER
    ############### --->


    <!--- ########## IF THE CAR REG IS TICKED SEARCH ONLY FOR CAR REG AND CAR REG
    2 FIELDS ############### --->
    <cfif isdefined('Form.CarReg')>
    <cfif #Form.CarReg# EQ "true" AND #SearchTerm# NEQ "" >
    AND
    EMPLOYEE_LOOKUP.CAR_REG LIKE '%#UCase(Trim(SearchTerm))#%'
    OR
    EMPLOYEE_LOOKUP.CAR_REG2 LIKE '%#UCase(Trim(SearchTerm))#%'

    </cfif>
    </cfif>
    <!--- ########## IF THE CAR REG IS TICKED SEARCH ONLY FOR CAR REG AND CAR REG
    2 FIELDS ############### --->

    ORDER BY
    FIRST_NAME, LAST_NAME
    ASC


    </cfquery>

    Matrix16 Guest

  2. Similar Questions and Discussions

    1. cfinclude problem with repeating regions
      Ok here is my issue. I have a template that that has cfinclude files for the menues and repeating region code blocks. These included files also have...
    2. Repeating Regions - Problem in Contribute 4
      I've created a page that has a number of repeating regions on. In Dreamweaver 8, they work like a charm. However, Contribute 4 is having a problem...
    3. repeating regions problem
      ciao, if i try to include a page that use repeating regions (using server side include) in a page that also use repeating regions, both not work, i...
    4. Repeating region bug/problem
      I have a repeating region right aligned within a cell that is on the right side of the page (the table is 100% width). Upon going into edit mode I...
    5. cfdocument problem with image repeating
      I am going to try posting this here since I got no repsonse in the reporting forum.... I've seen posted on this forum the issue of cfdocument...
  3. #2

    Default Re: SQL repeating rows Problem

    In two sections you need parenthesis around the conditions with OR between them.

    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS TICKED SEARCH
    FORENAME AND SURNAME ############### --->
    <cfif isdefined('Form.SurName') AND isdefined('Form.ForeName') >
    <cfif #Form.ForeName# EQ "true" AND #Form.SurName# EQ "true" AND
    #SearchTerm# NEQ "" >
    AND
    (EMPLOYEE_LOOKUP.FIRST_NAME LIKE '%#Trim(SearchTerm)#%'
    OR
    EMPLOYEE_LOOKUP.LAST_NAME LIKE '%#Trim(SearchTerm)#%')
    </cfif>
    </cfif>
    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS TICKED SEARCH
    FORENAME AND SURNAME ############### --->




    <!--- ########## IF THE CAR REG IS TICKED SEARCH ONLY FOR CAR REG AND CAR REG
    2 FIELDS ############### --->
    <cfif isdefined('Form.CarReg')>
    <cfif #Form.CarReg# EQ "true" AND #SearchTerm# NEQ "" >
    AND
    (EMPLOYEE_LOOKUP.CAR_REG LIKE '%#UCase(Trim(SearchTerm))#%'
    OR
    EMPLOYEE_LOOKUP.CAR_REG2 LIKE '%#UCase(Trim(SearchTerm))#%')

    </cfif>
    </cfif>

    draves Guest

  4. #3

    Default Re: SQL repeating rows Problem

    Hi DRAVEs,

    tried the parenthesis and still get exactly the same result.

    If i enter the search term of adam and have both forename and surname ticked
    it returns 107 records which are repeated records. Surname ticked only returns
    3 and forname ticked only returns 2.
    There are 34 site entries.

    Heres my modified sql.

    Sorry, any other ideas how to stop this from repeating.
    Matt



    <cfquery datasource="UKCHAPP7" name="qry_GetEmployeeData">
    SELECT

    EMPLOYEE_LOOKUP.FIRST_NAME,
    EMPLOYEE_LOOKUP.LAST_NAME,
    EMPLOYEE_LOOKUP.MIDDLE_INITIALS,
    EMPLOYEE_LOOKUP.JOB_TITLE,
    EMPLOYEE_LOOKUP.FAX_NUMBER,
    EMPLOYEE_LOOKUP.AREA,
    EMPLOYEE_LOOKUP.KO_NUMBER,
    EMPLOYEE_LOOKUP.EXT_NUMBER,
    EMPLOYEE_LOOKUP.DEPARTMENT,
    EMPLOYEE_LOOKUP.WORK_NUMBER,
    EMPLOYEE_LOOKUP.EMAIL,
    EMPLOYEE_LOOKUP.EMPLOY_NUMBER,
    EMPLOYEE_LOOKUP.MOBILE,
    EMPLOYEE_LOOKUP.CAR_REG,
    EMPLOYEE_LOOKUP.COMP_CAR,
    EMPLOYEE_LOOKUP.CAR_REG2,
    EMPLOYEE_LOOKUP.COST_CENTER,
    EMPLOYEE_LOOKUP.CAR_MAKE,
    EMPLOYEE_LOOKUP.ID,
    EMPLOYEE_LOOKUP.COMMENTS,
    EMPLOYEE_LOOKUP.DISPLAY_STATUS,
    EMPLOYEE_LOOKUP.DISPLAY_DATE,
    EMPLOYEE_LOOKUP.SAPID,
    SITES.ADDRESS_LINE1,
    SITES.ADDRESS_LINE2,
    SITES.ADDRESS_LINE3,
    SITES.ADDRESS_LINE4,
    SITES.ADDRESS_LINE5,
    SITES.ADDRESS_LINE6,
    SITES.TELEPHONE,
    SITES.FAX,
    SITES.SHORT_NAME,
    SITES.SITE_NAME,
    SITES.MAP_LINK,
    SITES.COUNTRY,
    SITES.LOC_PHOTO,
    SITES.DIRECTIONS_LINK


    FROM
    UKCHAPP7.EMPLOYEE_LOOKUP, UKCHAPP7.SITES
    WHERE
    EMPLOYEE_LOOKUP.DISPLAY_STATUS = 'Y'
    AND
    EMPLOYEE_LOOKUP.DISPLAY_DATE <= '#DateFormat(Now())#'

    AND
    EMPLOYEE_LOOKUP.SITE_LOCATION = SITES.SITE_NAME



    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS TICKED SEARCH
    FORENAME AND SURNAME ############### --->
    <cfif isdefined('Form.SurName') AND isdefined('Form.ForeName') >
    <cfif #Form.ForeName# EQ "true" AND #Form.SurName# EQ "true" AND
    #SearchTerm# NEQ "" >
    AND
    (EMPLOYEE_LOOKUP.FIRST_NAME LIKE '%#Trim(SearchTerm)#%'
    OR
    EMPLOYEE_LOOKUP.LAST_NAME LIKE '%#Trim(SearchTerm)#%')
    </cfif>
    </cfif>
    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS TICKED SEARCH
    FORENAME AND SURNAME ############### --->



    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS NOT TICKED
    SEARCH ONLY FOR FORENAME ############### --->
    <cfif isdefined('Form.SurName') AND isdefined('Form.ForeName') >
    <cfif #Form.ForeName# EQ "true" AND #Form.SurName# EQ "false" AND
    #SearchTerm# NEQ "" >
    AND
    EMPLOYEE_LOOKUP.FIRST_NAME LIKE '%#Trim(SearchTerm)#%'
    </cfif>
    </cfif>
    <!--- ########## IF THE FORENAME IS TICKED AND THE SURNAME IS NOT TICKED
    SEARCH ONLY FOR FORENAME ############### --->



    <!--- ########## IF THE FORENAME IS NOT TICKED AND THE SURNAME IS TICKED
    SEARCH ONLY FOR SURNAME ############### --->
    <cfif isdefined('Form.SurName') AND isdefined('Form.ForeName') >
    <cfif #Form.ForeName# EQ "false" AND #Form.SurName# EQ "true" AND
    #SearchTerm# NEQ "" >
    AND
    EMPLOYEE_LOOKUP.LAST_NAME LIKE '%#Trim(SearchTerm)#%'
    </cfif>
    </cfif>
    <!--- ########## IF THE FORENAME IS NOT TICKED AND THE SURNAME IS TICKED
    SEARCH ONLY FOR SURNAME ############### --->



    <!--- ########## IF THE koNumber IS NOT TICKED SEARCH ONLY FOR KONUMBER
    ############### --->
    <cfif isdefined('Form.KoNumber') AND isdefined('SearchTerm')>
    <cfif #Form.KoNumber# EQ "true" AND #SearchTerm# NEQ "">
    AND
    EMPLOYEE_LOOKUP.KO_NUMBER LIKE '%#LCase(Trim(SearchTerm))#%'

    </cfif>
    </cfif>
    <!--- ########## IF THE koNumber IS NOT TICKED SEARCH ONLY FOR KONUMBER
    ############### --->


    <!--- ########## IF THE CAR REG IS TICKED SEARCH ONLY FOR CAR REG AND CAR REG
    2 FIELDS ############### --->
    <cfif isdefined('Form.CarReg')>
    <cfif #Form.CarReg# EQ "true" AND #SearchTerm# NEQ "" >
    AND
    (EMPLOYEE_LOOKUP.CAR_REG LIKE '%#UCase(Trim(SearchTerm))#%'
    OR
    EMPLOYEE_LOOKUP.CAR_REG2 LIKE '%#UCase(Trim(SearchTerm))#%')

    </cfif>
    </cfif>
    <!--- ########## IF THE CAR REG IS TICKED SEARCH ONLY FOR CAR REG AND CAR REG
    2 FIELDS ############### --->

    ORDER BY
    FIRST_NAME, LAST_NAME
    ASC


    </cfquery>

    Matrix16 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