I have a report with 10 input parameters wherein only two are obligatory, the rest are optional. The problem is, as far as I know, if you use an input parameter that is blank in a WHERE condition of a SELECT statement no records will be selected from a table, so how to fix that ?
lets try something simple:
SELECT * FROM contrat WHERE article = 'sumsung' AND price = null
the user entered an article and did not enter a price
so the query will return nothing because price is null even when we have a contract with a sumsung article
If by blank you mean null or an empty string, you must construct for each of the optional parameters a condition like:
column_name = :optional_param OR :optional_param IS NULL
or:
column_name = :optional_param OR :optional_param = ''
So, your WHERE clause should look like:
WHERE <conditions for the non null parameters>
AND (column_name1 = :optional_param1 OR :optional_param1 IS NULL)
AND (column_name2 = :optional_param2 OR :optional_param2 IS NULL)
AND .......
Related
I have an SQL statement with a WHERE clause where I am adding additional filters if a variable passed into the stored procedure IN parameter is a certain value.
The IN parameter is called filterID and can be empty if I need all records, or have the value SEN,EAL or MAGT if I want to only select the records which have IDs in those columns from the LEFT JOINED tables.
This is the extract from the SQL query to show the joining of the tables and the additional filter.
LEFT JOIN tbl_sen ON tbl_pupil.pupilID = tbl_sen.pupilID
LEFT JOIN tbl_eal ON tbl_pupil.pupilID = tbl_eal.pupilID
LEFT JOIN tbl_magt ON tbl_pupil.pupilID = tbl_magt.pupilID
WHERE
tbl_test.schoolID=schoolID
AND (
(filterID='SEN' AND tbl_sen.senID IS NOT NULL) OR
(filterID='EAL' AND tbl_eal.ealID IS NOT NULL) OR
(filterID='MAGT' AND tbl_magt.magtID IS NOT NULL))
AND tbl_test.import=1;
WHen I execute the stored procedure with the values, it shows the correct results. However, if I don't want to add the filter and pass filterID as empty/NULL, it returns 0 results when I need it to return ALL the results.
Can anyone spot what I'm doing wrong?
...
AND CASE filterID WHEN 'SEN' THEN tbl_sen.senID IS NOT NULL
WHEN 'EAL' THEN tbl_eal.ealID IS NOT NULL
WHEN 'MAGT' THEN tbl_magt.magtID IS NOT NULL
ELSE filterID IS NULL END
AND ...
As I understand the filterID is local variable (SP parameter) so there is no problem to use it twice (rather than bound parameter of the query - in this case you may use user-defined intermediate variable).
Simplified DEMO
AND (
(filterID='SEN' AND tbl_sen.senID IS NOT NULL) OR
(filterID='EAL' AND tbl_eal.ealID IS NOT NULL) OR
(filterID='MAGT' AND tbl_magt.magtID IS NOT NULL) OR
(filterID IS NULL)
)
i have a database table with any column. and there is 3 form input in UI page, that a user can search a value in table by these inputs. the user can insert value for one of column (inputs), Two of columns or all of them. means its possible that one or two of inputs value be null and user don't use them for his search.
how to create the search query? because the following code have a problem. if user don't insert the value of all of 3 column, this query will take error.
$query="SELECT * FROM insurance WHERE nationalCode=:nationalCode AND insuranceNumber=:insuranceNumber
AND insuranceType=:insuranceType;";
in other words, i need to search in table across 3 columns if value of 3 these columns have define by user else if user have insert value for 2 of columns search will do by these two columns & if user have insert value only for 1 of column search will do by that 1 column.
If the user did not insert a value, say for nationalCode, I assume the you pass NULL (or an empty string?) as :nationalCode and in this case your condition should be:
nationalCode = :nationalCode OR :nationalCode IS NULL
or:
nationalCode = :nationalCode OR :nationalCode = ''
So for all the columns the statement should be:
SELECT *
FROM insurance
WHERE (nationalCode = :nationalCode OR :nationalCode IS NULL)
AND (insuranceNumber = :insuranceNumber OR :insuranceNumber IS NULL)
AND (insuranceType = :insuranceType OR :insuranceType IS NULL);
Dear #forpas i have used your query and don't have succeed. but with tiny change in your offerd Query a have arrived my gold.
i used both of your code :
nationalCode = :nationalCode OR :nationalCode = ''
and
nationalCode = :nationalCode OR :nationalCode IS NULL
indeed i changed your suggested Query as followin:
$query="SELECT * FROM insurance WHERE
(nationalCode=:nationalCode OR :nationalCode IS NULL OR :nationalCode='') AND
(insuranceNum=:insuranceNum OR :insuranceNum IS NULL OR :insuranceNum='') AND
(insuranceType=:insuranceType OR :insuranceType IS NULL OR :insuranceType='');";
so tanks for your help.
I want to edit my existing mySQL query based on whether the column show_only_on has a value or not.
If it has a value, then the correct query is the below
Select *
FROM alerts_data
WHERE id = 'abc' AND running_status='idle'
AND FIND_IN_SET("https://www.example.com/pricing", show_only_on)
And if it does not have a value, I just leave out the
AND FIND_IN_SET("https://www.example.com/pricing", show_only_on)
or make the FIND_IN_SET return true.
How can I do this using only this query and not any PHP ?
How about running it through an OR/AND statement?:
Select *
FROM alerts_data
WHERE id = 'abc' AND running_status='idle'
AND (
show_only_on IS NULL /* Whatever check you do to ensure it's not set */
OR FIND_IN_SET("https://www.example.com/pricing", show_only_on)
)
If I understand you correctly, you want to return all records where wither show_only_on is NULL or where show_only_on matches the FIND_IN_SET logic?
Sounds like this:
WHERE
id = 'abc' AND
running_status='idle' AND
(
FIND_IN_SET("https://www.example.com/pricing", show_only_on) OR
show_only_on IS NULL
)
Another option, one that I tend to use in parameterised queries:
WHERE id = 'abc'
AND running_status='idle'
AND COALESCE(show_only_on, "https://www.example.com/pricing") = "https://www.example.com/pricing"
Parameterised:
WHERE id = 'abc'
AND running_status = 'idle'
AND COALESCE(show_only_on, :url) = :url
Note this only works where show_only_on is NULL if and only if the value is considered empty/missing.
I want write search query which fetch the data from students table based on multiple conditions like by ID , by name and by date of birth like that.
if I use OR condition like
Select * from students where Id='101' or name='Kumar' or age='21';
it will return if any one field is entered. If multiple fields are entered it will consider first only.
If I use AND condition like
Select * from students where Id='101' and name='Kumar' and age='21';
It will return only if all fields are entered. If any one of the field is empty it will return zero, I mean it will return empty. I want to get result even some fields are empty.
I'm using this quarry for swing application if I haven't enter text in one of the field then it will become empty like I'd='' not null
How to get Result at this condition
Select * from students where Id='' and name='' and age='21'
I want to get result even some fields are empty.
Then include that in your conditions. Something like this:
WHERE
(Id = '101' OR Id IS NULL) AND
(name = 'Kumar' OR name IS NULL) AND
(age = '21' OR age IS NULL)
Nesting conditions in parentheses allows you to build fairly complex conditional logic. How you define that logic for finding the records you want to find is really up to you.
Use LIKE clause without %s. So it'll be acting as a simple =. Here's how it will work.
if(txtId.getText().equals("")){
String id = "%%";
}else{
String id = txtId.getText();
}
if(txtName.getText().equals("")){
String name = "%%";
}else{
String name = txtName.getText();
}
if(txtAge.getText().equals("")){
String age = "%%";
}else{
String age = txtAge.getText();
}
resultset = statement.executeQuery("SELECT * FROM student WHERE id LIKE '"+ id +"' and name LIKE '"+ name +"' and age LIKE '"+ age +"';");
Try this out. It'll work like a charm. (It's up to you to change the query according to your database)
I have a search page that has multiple fields that are used to create a refined search. Every field is optional. I'm trying to start crafting my sql query so that it will work given the proper variables but I'm having trouble.
Here is the SQL query I currently have:
SELECT
indicator.indid,
indicator.indicator,
indtype.indtype,
provider.provider,
report.report,
actor.actor
FROM
actor,
indicator,
indtype,
report,
provider
WHERE
indicator.indtypeid = indtype.indtypeid
AND indicator.actorid = actor.actorid
AND indicator.reportid = report.reportid
AND report.providerid = provider.providerid
AND indicator.indicator LIKE '%$indicator%'
AND indicator.indtypeid = $indtypeid;
Whenever I provide an indicator and an indtypeid, the search works just fine. However, when I leave the indtypeid field blank, and have the variable set to * (as its default value), the query returns no results. I've tried playing with the query manually and it doesn't seem to like the * or a % sign. Basically, if only an indicator is specified and no indtypeid is specified, I want to return all indicators for all indtypeids.
I'm sure I'm missing something minor, but I would appreciate any assistance that could be provided. I may be going about this all wrong in the first place.
Try this instead:
SELECT i.indid, i.indicator, it.indtype,
p.provider, r.report, a.actor
FROM actor a
INNER JOIN indicator i ON a.actorid = i.actorid
INNER JOIN indtype it ON i.indtypeid = it.indtypeid
INNER JOIN report r ON i.reportid = r.reportid
INNER JOIN provider p ON r.providerid = p.providerid
WHERE 1 = 1
AND ($indicator IS NULL OR i.indicator LIKE '%$indicator%')
AND ($indtypeid IS NULL OR i.indtypeid = $indtypeid);
So if you pass a $indicator = NULL, then the first condition AND ($indicator IS NULL OR i.indicator LIKE '%$indicator%') will be ignored since it will resolve to True, and the same thing for the second condition.
I've removed other Where condition and replace them with JOINs, and for WHERE 1 = 1 to make the query work fine in case you pass the two variables $indicator and $indtypeid with NULL values for each, in this case it will return all results since 1 = 1 always true.