|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Three table join problem
Hi!
I have three tables: tbl_hordepacks (H): hordepack_id user_id name tbl_hordepacks_creatures (HC): hordepack_id creature_id count tbl_user_collection_creatures (UCC): user_id creature_id count Given a hordepack_id and a creature_id, I want the count in the hordepack and the collection. So I need count from HC (easy) and count from UCC where creature_id matches the given one and the user_id matches the user_id of the given hordepack_id. I keep getting weird results that look like there's a problem doing SELECT hc.count AS packcount, ucc.count AS collectioncount. Do I need to do two different queries? My instinct is that there's a single query that will do this, but I'm really having a hard time finding it. Here's my current attempt: Code:
SELECT hc.count AS packcount, ucc.count AS collectioncount FROM tbl_hordepacks_creatures AS hc JOIN tbl_user_collection_creatures AS ucc ON hc.creature_id = ucc.creature_id JOIN tbl_hordepacks AS h ON h.user_id = ucc.user_id WHERE h.user_id = 1 AND h.hordepack_id = 1 AND hc.creature_id = 128 But this returns every count from HC without restricting by hordepack_id or user_id. UPDATE: Code:
SELECT hc.count AS packcount, ucc.count AS collectioncount FROM tbl_hordepacks_creatures AS hc JOIN tbl_user_collection_creatures AS ucc ON hc.creature_id = ucc.creature_id JOIN tbl_hordepacks AS h ON h.user_id = ucc.user_id WHERE h.user_id = 1 AND hc.hordepack_id = 1 AND ucc.creature_id = 128 This returns the correct result, but twice. I'm pretty sure I don't fully understand what's going on when I JOIN twice. Last edited by almo2001 : June 25th, 2009 at 03:05 PM. |
|
#2
|
|||||
|
|||||
|
Try this
sql Code:
__________________
Why do we always seek someone, something or some thought, are we afraid of ourselves? "The love of one's country is a splendid thing. But why should love stop at the border?" - Pablo Casals |
|
#3
|
||||
|
||||
|
i'd like to suggest a slight change to the query
the first table in the FROM clause should be the "driving" table, such that it determines the fewest rows to be joined to the other tables thus, conditions on the driving table belong in the WHERE clause, and all other conditions can/should be added to their respective ON clauses Code:
SELECT hc.COUNT AS packcount
, ucc.COUNT AS collectioncount
FROM tbl_hordepacks_creatures AS hc
INNER
JOIN tbl_user_collection_creatures AS ucc
ON ucc.creature_id = hc.creature_id
INNER
JOIN tbl_hordepacks AS h
ON h.hordepack_id = hc.hordepack_id
AND h.user_id = ucc.user_id
AND h.user_id = 1
WHERE hc.creature_id = 128
AND hc.hordepack_id = 1
|
|
#4
|
|||
|
|||
|
Quote:
So, the rows from the table in the FROM clause are first retrieved based on the WHERE clause (if given) and then the JOIN's are performed. Is that correct? |
|
#5
|
||||
|
||||
|
Quote:
i hope i did not give the impression that you can influence the sequence of execution by what you put into the WHERE clause for the first table in the JOIN you actually can dictate the sequence of join (with special keywords), but this is rarely a good idea the database optimizer will figure out the best execution sequence based on the logical construction of the query requirements, examining all the different ways of doing it, and choosing the fastest ... and it's way better at that than we SQL developers are ![]() all i wanted to say was that with a little bit of thought, it's pretty easy to tell in any given situation (and i've seen thousands of them) which table is the driving table so you write the query in that manner, thus adding ~immensely~ to the value of the SQL itself as a means of documention wtf the query is doing have you ever come back to a complex query several months later, and wished you knew what it was doing and why? that's what coding style is all about ![]() |
|
#6
|
|||
|
|||
|
Quote:
Ok, I think I get it. Thanks a lot for the help! ETA: one question: is "AND h.user_id = 1" redundant? As long as h.user_id matches ucc.user_id, it should be fine since ucc_user_id must be 1 since user 1 owns that pack... Last edited by almo2001 : June 29th, 2009 at 03:08 PM. |
|
#7
|
||||
|
||||
|
Quote:
|
|
#8
|
|||
|
|||
|
Quote:
WHERE hc.creature_id = 128 AND hc.hordepack_id = 1 One hordepack belongs to one user. So once we specify hordepack_id = 1, we have effectively specified the user since tbl_hordepacks has hordepack_id, user_id, and name. I've tried removing WHERE user_id = 1 and it seems to work the same. |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Three table join problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|