You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by Michael Schmitt <mi...@gmx.de> on 2005/03/06 14:48:36 UTC

Criteria: several columns OR-combined??

Hi,

I'm trying to create something like
SELECT * FROM table_a WHERE table_a.col_1='xxx' AND (table_a.col_2='bla' OR
table_a.col_3='blu')
with the Criteria class.

I tried something like this:

Criteria c = new Criteria();
c.add(TableAPeer.COL_1, 'xxx');
c.or(TableAPeer.COL_2, 'bla');
c.or(TableAPeer.COL_3, 'blu');
TableAPeer.doSelect(c);

but (of course) the  Method or() relates the given criterion to an existing
criterion for the same column.
For example, something like
...
c.add(TableAPeer.COL_1, 'xxx');
c.or(TableAPeer.COL_1, 'yyy');
...
will probably (not tested) result in
... WHERE ... AND (table_a.col_1='xxx' OR table_a.col_1='yyy') ...

I thought that I could do something like
1. create a new criterion
2. pass the (table_a.col_2='bla' OR table_a.col_3='blu') to the criterion
3. add the criterion to the criteria
but as far as I understand, a criterion always relates to a single column.

any ideas?

greetings,
michael


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


Re: Criteria: several columns OR-combined??

Posted by Michael Schmitt <mi...@gmx.de>.
I think you are half right.
I do not see any possibility to AND two criteria, but you can do it
with criterions.
I solved my problem in the following way:

Criteria c = new Criteria();
c.add(TableAPeer.COL_1, 'xxx');
Criteria.Criterion criterion = c.getNewCriterion(TableAPeer.COL_2, 'bla',
Criteria.EQUAL);
criterion.or( c.getNewCriterion(TableAPeer.COL_3, 'blu', Criteria.EQUAL) );
c.add(criterion);
TableAPeer.doSelect(c);

I don't know if there is a better way, but this approach seems to work.

greetings,
michael


----- Original Message ----- 
From: "Brandon" <ef...@hotmail.com>
To: "Apache Torque Users List" <to...@db.apache.org>
Sent: Sunday, March 06, 2005 8:05 PM
Subject: Re: Criteria: several columns OR-combined??


> If I remember correctly you can and two Criteria objects can't you?  Sorry
I
> don't have the source in front of me, it's on my laptop and it takes
forever
> to boot up, but I think you might be able to accomplish this by creating
two
> Criterias.  The first will have just your COL_1='xxx' and the second will
> consist of COL_2='bla' || COL_3='blu'    Then you should be able to AND
> those two Criteria objects together and get a result criteria from you can
> select on.  I might just be dead wrong, but I suppose it's worth a shot.
;)
> Good luck.
>
> -Brandon
>
>
> ----- Original Message ----- 
> From: "Michael Schmitt" <mi...@gmx.de>
> To: <to...@db.apache.org>
> Sent: Sunday, March 06, 2005 8:48 AM
> Subject: Criteria: several columns OR-combined??
>
>
> > Hi,
> >
> > I'm trying to create something like
> > SELECT * FROM table_a WHERE table_a.col_1='xxx' AND (table_a.col_2='bla'
> > OR
> > table_a.col_3='blu')
> > with the Criteria class.
> >
> > I tried something like this:
> >
> > Criteria c = new Criteria();
> > c.add(TableAPeer.COL_1, 'xxx');
> > c.or(TableAPeer.COL_2, 'bla');
> > c.or(TableAPeer.COL_3, 'blu');
> > TableAPeer.doSelect(c);
> >
> > but (of course) the  Method or() relates the given criterion to an
> > existing
> > criterion for the same column.
> > For example, something like
> > ...
> > c.add(TableAPeer.COL_1, 'xxx');
> > c.or(TableAPeer.COL_1, 'yyy');
> > ...
> > will probably (not tested) result in
> > ... WHERE ... AND (table_a.col_1='xxx' OR table_a.col_1='yyy') ...
> >
> > I thought that I could do something like
> > 1. create a new criterion
> > 2. pass the (table_a.col_2='bla' OR table_a.col_3='blu') to the
criterion
> > 3. add the criterion to the criteria
> > but as far as I understand, a criterion always relates to a single
column.
> >
> > any ideas?
> >
> > greetings,
> > michael
> >
> >
> > ---------------------------------------------------------------------
> > 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
>


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


Re: Criteria: several columns OR-combined??

Posted by Brandon <ef...@hotmail.com>.
If I remember correctly you can and two Criteria objects can't you?  Sorry I 
don't have the source in front of me, it's on my laptop and it takes forever 
to boot up, but I think you might be able to accomplish this by creating two 
Criterias.  The first will have just your COL_1='xxx' and the second will 
consist of COL_2='bla' || COL_3='blu'    Then you should be able to AND 
those two Criteria objects together and get a result criteria from you can 
select on.  I might just be dead wrong, but I suppose it's worth a shot.  ;) 
Good luck.

-Brandon


----- Original Message ----- 
From: "Michael Schmitt" <mi...@gmx.de>
To: <to...@db.apache.org>
Sent: Sunday, March 06, 2005 8:48 AM
Subject: Criteria: several columns OR-combined??


> Hi,
>
> I'm trying to create something like
> SELECT * FROM table_a WHERE table_a.col_1='xxx' AND (table_a.col_2='bla' 
> OR
> table_a.col_3='blu')
> with the Criteria class.
>
> I tried something like this:
>
> Criteria c = new Criteria();
> c.add(TableAPeer.COL_1, 'xxx');
> c.or(TableAPeer.COL_2, 'bla');
> c.or(TableAPeer.COL_3, 'blu');
> TableAPeer.doSelect(c);
>
> but (of course) the  Method or() relates the given criterion to an 
> existing
> criterion for the same column.
> For example, something like
> ...
> c.add(TableAPeer.COL_1, 'xxx');
> c.or(TableAPeer.COL_1, 'yyy');
> ...
> will probably (not tested) result in
> ... WHERE ... AND (table_a.col_1='xxx' OR table_a.col_1='yyy') ...
>
> I thought that I could do something like
> 1. create a new criterion
> 2. pass the (table_a.col_2='bla' OR table_a.col_3='blu') to the criterion
> 3. add the criterion to the criteria
> but as far as I understand, a criterion always relates to a single column.
>
> any ideas?
>
> greetings,
> michael
>
>
> ---------------------------------------------------------------------
> 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