Joining tables in cfquery

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

  1. #1

    Default Joining tables in cfquery

    Any help is greatly appreciated, thank you in advanced for your help!

    Database info:
    There are 2 tables.
    1. 1st table name: members
    ? id (pk)
    ? fname (first name)
    ? lname (last name)
    ? faddress (father address)
    ? fcity (father city)
    ? fzip (father zip)
    ? mon (yes/no available to work Monday)
    ? tue (yes/no available to work Tuesday)
    ? wed (yes/no available to work Wednesday)
    ? thu (yes/no available to work Thursday)
    ? fri (yes/no available to work Friday)
    ? sat (yes/no available to work Saturday)
    ? sun (yes/no available to work Sunday)

    2. 2nd table name: book_05
    ? id (pk)
    ? mem_id (member id)
    ? b_date (date for booked for work ?mm/dd/yyyy?)
    ? details (descriptions of event)
    ? post_by (admin)
    ? posted (timestamp)

    Table 1 member are all unique.
    Table 2 a single member can have many booked work days.

    What?s suppose to happen:
    I want to query to return all member name that have not been booked to work
    for the next day.

    The problem:
    The query return the name of a member that has been booked before as many time
    as they have worked. I only need the name to show once, please see code example
    below.

    My code:
    <cfparam name="startdate" default="#month(now())#/1/#year(now())#">
    <cfset nextday=dateadd('d',1,now())>

    <cfquery datasource="#db#" name="getSearch">
    select m.id, m.fname, m.lname, m.faddress, m.fcity, m.fzip, b.mem_id, b.b_date
    from members m left join book_05 b on m.id = b.mem_id
    where id = id
    AND b.b_date <> ###dateFormat(nextday, "mm/dd/yyyy")###
    </cfquery>


    <cfoutput>#getsearch.recordcount#</cfoutput><p> </p>
    <cfoutput query="getSearch">
    #fname# #lname#<br>
    </cfoutput>



    This is the out put:

    6
    Michelle Johnson
    Eric Del Rosario
    George Perez
    George Perez
    Veronica Gonzales
    Veronica Gonzales

    George Perez and Veronica Gonzales should only come up once. Even thought they
    have been booked to work twice before. I just want to know if they are booked
    for the next day.

    phamtum Guest

  2. Similar Questions and Discussions

    1. Joining three tables
      Hello all, I am having a bear of a time trying to join three tables in Sybase. Here is the query...... select DISTINCT(appl.ag_id),...
    2. Joining tables, Please help
      <b><u>Objective: </b></u> Join two DB tables to output two queries on a webpage. <b><u>Problem:</b></u> Error Diagnostic Information ODBC Error...
    3. Joining Multiple Tables
      I have a query that enables me to join various tables together. I managed to get all the data I need from all of the tables, aside from one, apart...
    4. joining 3 tables?
      helooo... i have 3 tables -Recipes, Ingredients and Products. Recipes table: RecipeID -PK Ingredients table: IngredientID -PK...
    5. joining 3 tables in dataset
      hi my problem is that i have to load 3 different tables from different databases into one dataset and do a join on all. i loaded all tables into...
  3. #2

    Default Re: Joining tables in cfquery

    Why are you doing this (where id = id) in your query?

    <cfquery datasource="#db#" name="getSearch">
    select m.id, m.fname, m.lname, m.faddress, m.fcity, m.fzip, b.mem_id, b.b_date
    from members m left join book_05 b on m.id = b.mem_id
    where id = id
    AND b.b_date <> ###dateFormat(nextday, "mm/dd/yyyy")###
    </cfquery>


    paross1 Guest

  4. #3

    Default Re: Joining tables in cfquery

    Please ignore the where id = id bit as I have loads of <CFIF statem,ents after for using this query as a search results generator. You can remove it if you like.
    phamtum Guest

  5. #4

    Default Re: Joining tables in cfquery

    I think there is another problem besides the dupes. Let's say there is a
    member that has never been booked before (i.e. there are no records for this
    member in the [book_05] table). That member will not be included in the
    results of your query.

    You're using a LEFT JOIN, to include all members (even if they have no
    bookings). Yet, in the WHERE clause you reference the booking date [b_date],
    which effectively excludes any member (that has never been booked) from the
    results.

    If you want to include all members in the results (even if they have never
    been booked), then I think you're looking for something like the query below. I
    don't know what database/connection type you are using. You may need to modify
    the syntax.



    select m.id, m.fname, m.lname, m.faddress,
    m.fcity, m.fzip, b.mem_id
    from members m left join book_05 b ON
    ( m.id = b.mem_id and b.b_date = #CreateODBCDate(nextday)# )
    where b.b_date is null

    mxstu Guest

  6. #5

    Default Re: Joining tables in cfquery

    It works, Thanks MXSTU you're the man!!! =D
    phamtum Guest

  7. #6

    Default Re: Joining tables in cfquery

    MXSTU,
    I hope you can help answer this question.

    The code above work great! But, now that I'm introducing another table that
    also needs to be join to the members table I'm getting an error "Missing
    expressions". The new tables' name is talents and the PK is mem_id only one row
    per member.

    Here is my code:
    select m.id, m.fname, m.lname, m.faddress, m.fcity, m.fzip, b.mem_id
    from members m left join book_05 b ON ( m.id = b.mem_id and b.b_date =
    #CreateODBCDate(nextday)# ),
    members m left join talents t on m.is = t.mem_id
    where b.b_date is null

    Thank you in advanced fopr your assistance!

    phamtum Guest

  8. #7

    Default Re: Joining tables in cfquery

    The syntax of your FROM clause is a little off. I did not test this code, but
    I think what you're looking for is something like this. Modify it accordingly
    for you database type. You may want to review your database documentation for
    the exact JOIN syntax

    select m.id, m.fname, m.lname, m.faddress
    from ( (members m left join book_05 b ON
    ( m.id = b.mem_id and b.b_date = #CreateODBCDate(nextday)#) )
    left join talents t on m.id = t.mem_id
    where b.b_date is null

    Is a LEFT JOIN with the Talents table what you want (i.e. return all member
    records even if there is no matching record in the Talents table)?



    mxstu Guest

  9. #8

    Default Re: Joining tables in cfquery

    Brilliant!!!
    You have knocked every curved ball that I?ve thrown at you out of the park! Thank you very much for your expertise advises!!!
    phamtum 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