tags:

views:

54

answers:

1

Table structure goes something like this:

Table: Purchasers Columns: id | organization | city | state

Table: Events Columns: id | purchaser_id

My query:

SELECT purchasers.*, events.id AS event_id 
FROM purchasers 
INNER JOIN events ON events.purchaser_id = purchasers.id 
WHERE purchasers.id = '$id'

What I would like to do, is obviously to select entries by their id from the purchasers table and join from events. That's the easy part. I can also easily to another query to get other purchasers with the same organization, city and state (there are multiples) but I'd like to do it all in the same query. Is there a way I can do this?

In short, grab purchasers by their ID but then also select other purchasers that have the same organization, city and state.

Thanks.

+1  A: 

You could try something like

SELECT  p.*, 
         e.id
FROM    purchasers p  INNER JOIN 
        events e ON e.purchaser_id = p.id  INNER JOIN
        (
            SELECT  p.*
            FROM    purchasers p
            WHERE   p.id = '$id'
        ) Original  ON  p.organization = Original.organization
                    AND p.city = Original.city
                    AND p.state = Original.state

The subselect Original will return the original purchaser, and then link to the purchasers table by organization, city and state

EDIT:

Changed the query, this will still return duplicates, but only for the number of events registered per purchaser. If you wish to retrieve a DISTINCT list of purchasers, you cannot do this with the event id, so you need something like

SELECT  p.*
FROM    purchasers p  INNER JOIN 
        (
            SELECT  p.*
            FROM    purchasers p
            WHERE   p.id = '$id'
        ) Original  ON  p.organization = Original.organization
                    AND p.city = Original.city
                    AND p.state = Original.state
astander
@astander that is very impressive sql! Thanks for helping out. With the record I'm testing, there are 2 events associated with one of the purchasers, then 2 more duplicate purchasers with the same name, city and state (I want the ability to merge purchasers). Your example returns the same purchaser 4 times (2 results for each event). Thoughts?
jeerose
Are you just looking for the DISTINCT list of pruchasers, WITHOUT the events?
astander
I'm looking for a) the purchaser who's ID has been called b) check to see which events are associated with it and c) other purchasers with the same organization, city, state. Eventually I also need to check and see if those other duplicate purchasers have any events attached to them as well.
jeerose
Seeing as purchasers to evets is a 1 to many link, you cannot retrieve the event id without having duplicate purchases, as I have shown in the edited 1st sql query. The only other way might be to use group_concat to show the event ids in a single column, which would remove the duplicate purchasers. http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
astander
Thanks again - i'll have a look at this and get back.
jeerose
Astander this is fantastic. I ended up going with your second option. Very helpful.
jeerose