*
The moose likes JDBC and the fly likes AND/OR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "AND/OR" Watch "AND/OR" New topic
Author

AND/OR

Mallika Kumar
Ranch Hand

Joined: Feb 15, 2001
Posts: 61
Hi,
I'm creating a query based on two values entered by the user. If only the first value is entered, I'm creating a query based on that. If only the second value is entered, I'm getting the appropriate query. But when both are entered, I'm creating a query that says
select a.colA,b.colB,c.colD,d.colD from tableA a,tableB b,tableC c,tableD d where a.colA like ? AND a.colB like ?;
In the prepared statement, I'm passing the values for the colA and colB as "%"+colAvalue+"%" and "%"+colBvalue+"%".
So, this query gets rows if both the conditions match. But if I try to make it OR and run the query, I get a join,a Cartesian join,with A*B*C*D number of rows (with A rows in tableA,B rows in tableB,C rows in tableC and D rows in tableD). How can I modify the select statement so that I can achieve an OR operator and not AND ?
Am I missing something here ? Any suggestions will really be very helpful.
Thanks a lot.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You need to specify join criteria for each of the 4 tables you are including in your SQL. Something like:
select a.colA,b.colB,c.colD,d.colD from tableA a,tableB b,tableC c,tableD d where a.colA = b.colB and a.colA = c.colD and a.colA = d.colD and (a.colA like ? or a.colB like ?);


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Mallika Kumar
Ranch Hand

Joined: Feb 15, 2001
Posts: 61
Thanks a TON THOMAS, it worked. Gosh, its great to see responses to all questions here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AND/OR