sql query to incrementally modify where condition until result contains what is required
- by iamrohitbanga
I need an sql query to select some items from a table based on some condition which is based on a category field.
As an example consider a list of people and I fetch the people from a particular age group from the database. I want to check if the result contains at least one result from each of a number of categories. If not I want to modify the age group by extending it and check the results again. This is repeated until I get an age group for which one result is present for each category. Right now i am doing this by analyzing the results and modifying the sql query. So a number of sql select queries are sent. What is the most efficient way of doing this?
I am invoking the select queries from a java program using jdbc. I am using mysql database.