Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7727939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:35:26+00:00 2026-06-01T05:35:26+00:00

I like to have a query like this: select data from table where (x

  • 0

I like to have a query like this:

select data from table
 where (x > 1 and x < 100)
    or (x > 250 and x < 300)

In ORMlite, that’s possible using this code:

final QueryBuilder<Data,Integer> qb = queryBuilder();
final Where<Data, Integer> w = qb.where();

w.or(
    w.gt("x", 1).and().lt("x", 100),
    w.gt("x", 250).and().lt("x", 300)
)

While thats great if one knows the conditions beforehand & at the time of coding, I need the conditions to be dynamically added.

Basically that method public com.j256.ormlite.stmt.Where<T,ID> or(com.j256.ormlite.stmt.Where<T,ID> left, com.j256.ormlite.stmt.Where<T,ID> right, com.j256.ormlite.stmt.Where<T,ID>... others) is not enough.
It needs another or method that supports a ArrayList of Where conditions.

Thanks for any suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:35:27+00:00Added an answer on June 1, 2026 at 5:35 am

    While thats great if one knows the conditions beforehand & at the time of coding, I need the conditions to be dynamically added.

    In ORMLite Where.or(Where<T, ID> left, Where<T, ID> right, Where<T, ID>... others) is a bit of a syntax hack. When you call:

    w.or(
        w.gt("x", 1).and().lt("x", 100),
        w.gt("x", 250).and().lt("x", 300)
    );
    

    What the or() method gets is:

    w.or(w, w);
    

    You really could rewrite it as:

    w.gt("x", 1).and().lt("x", 100);
    w.gt("x", 250).and().lt("x", 300);
    w.or(w, w);
    

    The or method there is only using the arguments to count how many clauses it needs to pop off of the stack. When you call gt and lt and others, it pushes items on a clause stack. The and() method pulls 1 item off the stack and then takes another item in the future. We do these syntax hacks because we want to support linear, chained, and argument based queries:

    w.gt("x", 1);
    w.and();
    w.lt("x", 100);
    

    versus:

    w.gt("x", 1).and().lt("x", 100);
    

    versus:

    w.and(w.gt("x", 1), w.lt("x", 100));
    

    But this means that you have the power to simplify your code immensely by using the Where.or(int many) method. So in the or example above can also be:

    w.gt("x", 1).and().lt("x", 100);
    w.gt("x", 250).and().lt("x", 300);
    // create an OR statement from the last 2 clauses on the stack
    w.or(2);
    

    So you don’t need the conditions list at all. All you need is a counter. So you could do something like:

    int clauseC = 0;
    for (int i : values) {
        if (i == 1) {
            w.le(C_PREIS, 1000);
            clauseC++;
        } else if (i == 2) {
            w.gt(C_PREIS, 1000).and().le(C_PREIS, 2500);
            clauseC++;
        } else if (i == 3) {
            w.gt(C_PREIS, 2500).and().le(C_PREIS, 5000);
            clauseC++;
        } else if (i == 4) {
            w.gt(C_PREIS, 5000).and().le(C_PREIS, 10000);
            clauseC++;
        } else if (i == 5) {
            w.gt(C_PREIS, 10000);
            clauseC++;
        }
    }
    // create one big OR(...) statement with all of the clauses pushed above
    if (clauseC > 1) {
        w.or(clauseC);
    }
    

    If i can only be 1 to 5 then you can just use values.size() and skip the clauseC. Notice that if we are only adding one clause then we can skip the OR method call entirely.

    Oh, and the following statement will not work:

    target.or().raw(first.getStatement());
    

    because target and first are the same object. first.getStatement() dumps the entire SQL WHERE clause which I don’t think is what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query like this: SELECT TABLE_NAME, COLUMN_NAME, IS_NULLABLE, DATA_TYPE FROM MY_DB.INFORMATION_SCHEMA.COLUMNS WHERE
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I have a query like this: SELECT t1.id, (SELECT COUNT(t2.id) FROM t2 WHERE t2.id
I have a query like this: SELECT COUNT(*) AS amount FROM daily_individual_tracking WHERE sales
I have a query like this SELECT TOWN, NAME FROM CINEMA WHERE CITY_ID =
I have a query like this SELECT user_id FROM user WHERE user_id NOT IN
So lets say i have a query like this SELECT a as d,b,c FROM
I have a simple query like this: select * from mytable where id >
I have a query in oracle to fetch data from table using rownum but
Say I have a query like this: select ((amount1 - amount2)/ amount1) as chg

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.