If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > getting to grips with UNION

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-15-09, 10:31
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
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?
Reply With Quote
  #2 (permalink)  
Old 05-16-09, 05:31
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
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
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On