Note that there are some explanatory texts on larger screens.

plurals
  1. POBest Practice - Building a query based on user selections
    primarykey
    data
    text
    <p>I am creating a small web based tool that allows users to query a large lookup table in various ways. Since its a small tool, I'm using JSP/Servlets. </p> <p>The lookup table has a definition something like this:</p> <p>column1 | column2 | column3 | date | user | count</p> <p>The user can query by column values or a range. Also, the results can be sorted by a specific column. Because the table has several hundred thousand of records, and growing rapidly, I'm using Oracle's ROWNUM and only returning a small subset of results.</p> <p>I have a form page that gets the search criteria from the user. I generate my query based on a series of conditions, for example:</p> <pre><code>query = "SELECT * FROM mytable WHERE 1=1 " if(searchCriteria1 != "") query += "AND column1='searchCriteria1' " if(searchCriteria2 != "") query += "AND column2='searchCriteria2' " if(searchCriteria2 != "") query += "AND column2='searchCriteria2' " if(searchCriteria3 != "") query += "AND column3='searchCriteria3' " if((searchCriteria4 != "") &amp;&amp; (searchCriteria5 != "")) query += "AND date&gt;='searchCriteria4' AND date&lt;='searchCriteria5' " etc... </code></pre> <p>(This is just simplified pseudo code)</p> <p>The sorting is handled after the first results page is displayed. The user clicks on the column header of that page to sort by that column. This would be doing a post back and querying the database. Basically, I run the same code above but with this at the end:</p> <pre><code>if(sortColumn1) query += "ORDER BY column1" if(sortColumn2) query += "ORDER BY column2" if(sortColumn3) query += "ORDER BY column3" </code></pre> <p>So, as you might imagine, my query building code is very long with all these different conditions. Any suggestions on a better way to do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload