Querying data that matches in two different tables

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

  1. #1

    Default Querying data that matches in two different tables

    The code is attached. Basically there are two tables, contact and recruiter.
    When you initially add someone to contact, it also adds certain information to
    the recruiter table along with the primary id just writen to the contact
    table... confused yet? Basicaly, the attached code shows a search of the
    database on form submission. I can't figure out how I get it to search the
    first one and show only the ones where they are in the first db, and the second
    db based on primary ids I'd assume.

    Anybody have an idea?

    <cfquery name="grabusers" username="sla" password="slaera" datasource="sla">
    SELECT *
    FROM contact
    where <cfif form.firstname eq ""><cfelse>fname like
    '%#form.firstname#%'</cfif><cfif form.lastname eq ""><cfelseif form.firstname
    eq "">lname like '%#form.lastname#%'<cfelse>and lname like
    '%#form.lastname#%'</cfif>
    ORDER BY lname asc;
    </cfquery>

    weswhite7 Guest

  2. Similar Questions and Discussions

    1. Querying related tables
      Simple query question - I have a real estate table with various fields (Beds, Baths, SqFt, etc). One of the fields is BrokerID, which is foreign...
    2. Querying one-to-many tables
      Hi, I have a database with, for the sake of simplicity, two tables with a one-to-many relationship. The first table is the user info and the...
    3. querying FileMaker data
      Thanks, I'll try that.
    4. Querying for unmatched records in two tables
      I have two tables - One contains a list of all the zip codes in the US. The other table is basically a mailing list containing addresses of people...
    5. querying a view of a lot tables
      I have setup a database to record our IP Account from ALL of our routers in a form. It's very slow and sometimes times out.. which is NO GOOD.. I...
  3. #2

    Default Re: Querying data that matches in two different tables

    select c.*, r.*
    from contact c, recruiter r
    where c.id=r.id

    this will return all columns joined on id. you can change the select to
    return only those columns you are interested in from each table.

    d.

    warddc Guest

  4. #3

    Default Re: Querying data that matches in two different tables

    ok that makes sense, the problem is im searching based on a first and last name
    input. so if i put in wes, it pulls up anyone with "wes" as their name. that
    comes from the contact table. so then how do i take that output and match it to
    only where that id is present in teh recruiter table?

    weswhite7 Guest

  5. #4

    Default Re: Querying data that matches in two different tables

    Originally posted by: weswhite7
    ok that makes sense, the problem is im searching based on a first and last
    name input. so if i put in wes, it pulls up anyone with "wes" as their name.
    that comes from the contact table. so then how do i take that output and match
    it to only where that id is present in teh recruiter table?

    You need to add an extra condition to the where clause provided warddc:

    --- psuedo-code
    select (some columns)
    from contact c, recruiter r
    where c.id=r.id AND
    c.fName = 'Wes'

    Just don't use SELECT * , as in your original post. It requires more work by
    the database and usually returns more information than you actually need, which
    can affect performance. It can also cause problems with your CF output, when
    joining two or more tables that share some of the same column names.



    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