I have 3 tables that I am joining together based on that fact that one (or
more) values appear in the 2nd table, but those values that need to appear
are garnered from the 3rd table (and i want the id that is associated by
the 1st table).  Err.... well, an example would be better:

Table 1:

id -> int
whatiwant -> int

Table 2:
id -> int
table1_id -> int
table3_id -> int

Table 3:
id -> int
value -> varchar

The relationship is for every record in table 1, you can have 0 to (as
many different records their are in table 3) in table 2, and Table 3 is a
seperate table that just holds a bunch of values.

My query would be then, as such:

## SELECT
##    COUNT(table1.id) AS total,  
##    table1.whatiwant
## FROM
##    table1,table2,table3 
## WHERE
##    table2.table1_id = table1.id    
##  AND
##        (table3.id = table2.table3_id 
##      AND
##            (table3.value = 'some' 
##          OR table3.value = 'needed'
##          OR table3.value = 'values'
##            )
##        )
## GROUP BY
##    table1.id  
## HAVING
##    total = 3;

which basiclly is a way to get a correspondance between table3 and table1
via table2.

What this will give me are all the record in table1 which have all the
values i want from table3 associated via table2 (there can be more then 3,
or less then 3).  And the value I want is that record in table1 that
satifies all those requirements.

The above query does work.  BUT!!! am I doing something laughably wrong?
This query requires that for each "OR" statement in table3, all values
are looked at in table2, then grouped, and then the only ones returned are
those that group to the same number of OR's we are looking for.  I have
got to think this is inefficient.

Anyone have suggestions?  Comments?  Anything?

Thanks,
--jim


 --------
When I read about the evils of drinking, I gave up reading.


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <mysql-unsubscribe-##L=##[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to