Utilizing AND Operators and Wildcards in WHERE Statements for Various Conditions

On my screen, there are three input fields for capturing user search criteria. These fields are not mandatory and the input values are used as parameters in the SQL WHERE clause. Here is an example:

SELECT col1, col2, col3, col4  
FROM table LEFT JOIN otherTable ON tableID = otherTableID
WHERE col1 LIKE :d1 AND col2 LIKE :d2 AND col3 LIKE :d3

User data is collected from the browser like this:

var inputField1= "%" + jQ.trim(jQ(".someclass").val()) + "%";
var inputField2= "%" + jQ.trim(jQ(".someclass").val()) + "%";
var inputField3= "%" + jQ.trim(jQ(".someclass").val()) + "%";

This data is then used in a Presto.ExecuteSql object:

Presto.ExecuteSql(
{
    name: "SQL_Query",
    params:
    {
        'd1': inputField1,
        "d2": inputField2,
        "d3": inputField3,
        "d4": inputField4,
    },

When users enter data in all three fields, the query returns the correct number of records. However, when they only fill in one field, the number of records returned is incorrect. How can I modify the WHERE clause to always get the right amount of records?

Answer №1

To circumvent null parameters, utilize boolean logic as follows:

if (:param1 is null or field1 like :param1)
  and (:param2 is null or field2 like :param2)
  and (:param3 is null or field3 like :param3)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to incorporating a jade file into a node.js application after executing an Ajax request

While working on my node.js application, I encountered an issue with loading a new HTML file using Ajax on a button click. Despite everything else working perfectly, the new HTML file was not being loaded. This is how I am making the ajax call in my main. ...

How can I interact with objects in a Three.js scene? Is document.getElementById() the appropriate method to use?

for ( var j = 0; j < 100; j ++ ) { var particles = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: 0x666666, program: programStroke } ) ); particles.position.x = Math.random() * 800 - 400; particles.position.y = Math.random() ...

Unveiling the essence of Lodash's differenceBy functionality

I am facing a challenge with two arrays of objects. I need to identify the differences between the newData and oldData arrays based on their identifiers. Specifically, I want to display objects from oldData whose identifiers are not present in newData. Her ...

Error: [BITFIELD_INVALID_RANGE]: The bitfield flag or number entered is not valid: 3214336

Currently working on a Discord Dashboard project, but encountering an unusual error: Invalid bitfield flag or number 3214336. This issue arises when attempting to retrieve the guilds that a user has MANAGE_GUILDS permission for. Below is the snippet of my ...

Guide on how to send a variable to the footer section in Jade using Node.js

In my footer with Jade, I want to show a variable named total. .container .footer hr(style='margin: 30px 0 10px 0;') p #{total} entries in this database. link(rel='stylesheet', href='/css/style.css') scri ...

Hiding the y-axis on a msstackedcolumn2dlinedy Fusion Chart: A step-by-step guide

Currently, I am utilizing the msstackedcolumn2dlinedy fusion charts. To see an example of this in action, please take a look at this fiddle: Fiddle The objective I have is to hide the y-axis value on the right side of this chart. Can anyone provide guida ...

I'm encountering an issue in my server.js file where I am unable to read the property 'collection' as it is undefined

I have encountered an error in my code: /home/ubuntu/workspace/server.js:43 db.collection('quotes').find().toArray(function(err, results) { ^ TypeError: Cannot read property 'collection' of undefined at Object.<anonymous> ( ...

Failed installation of Semantic-ui through npm encountered

Encountering an error while attempting to install semantic-ui through npm for a new project. Here are the version details: $ node -v v16.14.0 $ npm -v 8.10.0 $ npm i semantic-ui npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_em ...

What is the method to submit post data using jQuery or JavaScript?

I need help creating a button that can send post data to another location. Is there a way I can achieve this using JavaScript, or Ajax/jQuery? If there are any details missing from my request, please let me know. ...

Instructions on removing the initial value in a column and changing the remaining values to dates

In my SQL Server database, I have a column named "col1" with the following values: col1 ----- 1210607 1191011 1200101 I want to convert this column to a date column by transforming the values as follows: 1210607 becomes 2021-06-07 1191011 becomes 2019-10 ...

Find all Mondays occurring within a specified date range using Moment.js

I need to extract all Mondays within a specific date range. let start = moment(this.absence.FromDate); let end = moment(this.absence.ToDate); The user has the option to deactivate certain weekdays during this period by setting booleans. monday = true; t ...

Vue.js - the value of this is not defined

<div class="content-chart"> <chart :type="'line'" :data="lineData" :options="options"></chart> </div> In the template section above, a component is defined to render a chart using Chart.js. The script below handles the l ...

Ensuring Sequential AJAX Calls in jQuery for Optimal Performance

I recently transitioned some code to FastCGI for backend processing of AJAX requests from a jQuery-driven front end. While FastCGI has generally sped up the process, I've encountered a performance drawback when two jQuery AJAX requests are made in rap ...

Modify the CSS class dynamically by clicking a button (using class names stored in an array)

How can I dynamically change a CSS class on button press while using an array of class names? When I hard code the classes in a switch statement, everything works fine. However, when trying to pull class names from an array, it jumps to the end of the swit ...

The console indicates that the state's arrays have been filled, yet I'm unable to retrieve the object[0]

In my code, the functions that populate the state are called on component will mount. When I log the state on the render, this is what I observe in the log. The log clearly shows that the arrays in the state have been populated, although there seems to be ...

Ways to transmit data to the backend using React.js

I am completely new to backend programming and feeling lost when it comes to sending onClick data to the backend for storage. Currently, the backend is a basic flask server, but there is a possibility that my partner might switch it to mySQL. My project is ...

Guide on swapping out a script file load with an Ajax request in JavaScript (utilizing YQL and JavaScript)

!!! The question below has been answered with a great example by Andrew Whitaker. Essentially, you can use the $.getJSON(..) method to fetch JSON data programmatically from a service like YQL. It's important to note that this service may be associated ...

The Fancybox iFrame is not appearing on the screen

I am facing an issue with the html and javascript code I have. The html looks like this: <ul> <a class="iframe" href="/posting/form?id=8"><li>Publish</li></a> </ul> and I am using the following javascript: <scr ...

Is it achievable to selectively target a particular class without resorting to utilizing an ID within DOM manipulation?

Is there a way to apply a function to a specific class without requiring an id? For example, in CSS hover effects, where multiple elements share the same class but only the one being hovered over is affected. function toggleHeight(elementId, arrowId) { ...

Preserving Search and Filter Settings in jQuery Data Tables

I've been working with a datatable and I'm trying to figure out how to keep dropdown filters and search parameters saved when the page is reloaded, just like shown in the screenshot below. However, I also want these parameters to be cleared if th ...