db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ludwig Magnusson" <lud...@itcatapult.com>
Subject RE: Multiple addJoin with OR
Date Mon, 09 Mar 2009 11:06:42 GMT
Hello again!
Sorry I haven't replied for a while.
I solved the problem however by adding a custom part to the criteria:

criteria.add(CategoryPeer.ID, (Object) String.format("(%s=%s OR %s=%s)", 
CategoryPeer.ID, SoldProductPeer.CATEGORY_ID, 
CategoryPeer.ID, BoughtProductPeer.CATEGORY_ID), 
SqlEnum.CUSTOM);

This gave me the result I wanted. Not perfect, but it works.
/Ludwig

-----Original Message-----
From: Marc Kannegie├čer [mailto:macmac@gmx.org] 
Sent: den 2 mars 2009 21:41
To: Apache Torque Users List
Subject: Re: Multiple addJoin with OR

Ludwig Magnusson wrote:
> It does make sense. I rewrote the query by hand (changed AND to OR) and
> executed it in the MySql console and it gave the response I wanted.
> 
> The situation is kind of like this:
> Table a is a category table, it only contains ids and names of categories.
> Table B is a "products sold" table, and table C is a "products bought"
> table. All products belong to a certain category, and I want a query that
> gives me all the categories that a certain user has sold/bought products
> from.
> 
> /Ludwig

I still don't think it makes sense using joins here:

When joining tables you usually "combine" rows of multiple tables
(interested in results from both tables). What you're trying to do is
selecting only categories (from table a) and you don't seem to be
interested in the corresponding entries in tables b and c.
It really seems a subquery is the right thing to use here:

SELECT * FROM category a WHERE EXISTS ( SELECT * FROM bought b where
a.id =  b.fk ) OR EXISTS ( SELECT * FROM sold c where a.id = c.fk);


Im also not sure if your join works if you'd use the ON-Clause that is
usually ment for join-conditions instead of WHERE:

SELECT * FROM a JOIN b ON a.id = b.fk OR JOIN c ON a.id = c.id
OR is not allowed here ---------------^

If you execute the statement you describe the DBMS (IMHO!) actually
produces a Cartesian product and applies the WHERE-Conditions "afterwards":
SELECT Category.* from a, b, c WHERE a.id = b.fk OR a.id = c.fk

This means (no DBMS-internal optimization assumed) the DBMS first
"combines" each row of a with all rows of b and then combines all of
these rows to all rows of c. This means you'll have a "virtual table" of
rows_a * rows_b * rows_c size which get reduced by the WHERE-Clause
afterwards. No real "join" as you try to approach, i think ;)

See for example http://dev.mysql.com/doc/refman/5.0/en/join.html for
more information about JOINs

Just my 2ct.

Greetings,

Marc


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Mime
View raw message