Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2002
    Posts
    189

    Unanswered: getting to grips with UNION

    Trying to mash together results from two tables: EventTemplates, and UserEvents.

    EventTemplates is just full of data about an event - date, title etc. When a user participates in an event, my PHP application pulls the required record out of EventTemplates and drops it into a form. The user is then free to edit all or some of these fields. When they submit the form, all that data is dropped into UserEvents, along with their userID.

    So UserEvents is just a clone of EventTemplates, with a UserID field.

    For a calendar feature, I want to pull out everything from EventTemplates, coupled with everything for that user from UserEvents.

    Trouble is, when I do that I get duplicate rows.

    This is what I've done:

    Code:
    SELECT
      idUserEvents,
      eventName,
      idEventTemplates
    FROM
      UserEvents
    WHERE idUsers = 19
    
    UNION
    
    SELECT
      idEventTemplates
      eventName,
      idEventTemplates
    FROM
      EventTemplates
    I need to get that idUserEvents field out of UserEvents, and I need to get that idEventTemplates field out of EventTemplates.

    But if a user's added an event using that event template, then I get two rows back for it; one from each table. The UNION doesn't see them as distinct because it's comparing the id's from two different tables.

    Am I going about this all wrong?

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    It may be clearer to understand what you class as a duplicate if you show us some of your output, highlighting the offending rows.
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •