Select null or not null depend on parameter - mysql

I have a query like this:
SELECT old.NPP,
old.NAMA,
old.JOB,
pgh.status
FROM a old
LEFT JOIN b pgh ON old.person_id = pgh.person_id
AND pgh.periode = 2015
AND pgh.LAST_STATUS = 1
WHERE old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI'
AND pgh.status = #status
ORDER BY pgh.status
i want my query can get every value from pgh status, like when i type status 5, the query must to return status is 5, but when i input null, the query will return data from database when the status is null.
how query will be set?

You can write the condition this way:
where old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI' and
(pgh.status = #status or pgh.status is null and #status is null)

If i understand your situation correctly, i think there is empty strings, so to make sure the condition is correct. just use ISNULL
SELECT old.NPP,
old.NAMA,
old.JOB,
pgh.status
FROM a old
LEFT JOIN b pgh ON old.person_id = pgh.person_id
AND pgh.periode = 2015
AND pgh.LAST_STATUS = 1
WHERE old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI'
AND pgh.status = ISNULL(#status,'')
ORDER BY pgh.status

Related

MySQL to filter result by using where & and condition

I'm trying to fetch some rows from a question table in MySQL like this:
SELECT *
FROM raise_question
where documentID = '1'
and delReq != 1 OR delReq IS NULL
However, some rows have different documentID. (Please see the picture). Is this a correct way (using HAVING instead of AND) to return the result I want ( which is it only contains specific documentID):
SELECT *
FROM raise_question
where documentID = '1'
having delReq != 1 OR delReq IS NULL
With AND query returns:
The expected result (only contains one documentID) after using Having I want is like this:
Your WHERE translate to
where (documentID = '1'
and delReq != 1) OR delReq IS NULL
, because AND has higher precedence than OR
Put bracket in your OR statement.
where documentID = '1'
and (delReq <> 1 OR delReq IS NULL)
Is this what you want?
select distinct documentid
from raise_question
where delReq <> 1 or delReq is null;
This returns document ids that have a non-1 delreq value.

MySQL IF returning nothing

I am trying to do some optimisation, currently post mysql work is done on the results to set a new paramter $class_subject... so i am trying get this already calculated in mysql...
SELECT
class_grade.results as results,
subjects.subject as subject,
subjects_pseudonyms.pseudonym as pseudonym,
IF( subjects_pseudonyms.pseudonym = null, subjects.subject, subjects_pseudonyms.pseudonym ) as class_subject
FROM
class_grade
INNER JOIN class ON class_grade.class_ID = class.class_ID
INNER JOIN subjects ON class.subject_ID = subjects.a_ID
LEFT JOIN subjects_pseudonyms ON class.subject_pseudonym_ID = subjects_pseudonyms.a_ID
WHERE
class_grade.teacher_ID = :teacher_id AND
class_grade.class_ID = :current_class_ID AND
class_grade.report_set_ID = :report_set_ID AND
class_grade.student_ID = :current_student_ID
In the above query the pseudonym might be null, if so I am attempting to set a new variable class_subject to be either subject or pseudonym...
The query runs fine, a results example is:
[results] => 71
[subject] => Law
[pseudonym] =>
[class_subject] =>
The problem is, the class_subject is not being populated..
Is there something wrong with my IF() cond?
Thanks,
John
You need to use IS NULL instead of = NULL or ISNULL()
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_isnull
ISNULL() can be used instead of = to test whether a value is NULL.
(Comparing a value to NULL using = always yields false.)

WHERE Clause only IF NOT NULL

I need help with my SELECT.
I got a field that can be NULL and in it there is stored a foreign-key.
SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerBrandID = beerID <--- this can be NULL
So if it's NULL nothing happens.
How can I check if beerID is NOT NULL so I can use "beerBrandID = beerID"?
You probably need something like this:
First example:
SELECT * FROM beerImage WHERE beerBRewID = brewID AND (beerID IS NULL OR beerBrandID = beerID)
Second Example:
SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerID IS NOT NULL AND beerBrandID = beerID
The first example will allow you to show records which have beerID null along with the beers which beerBrandID is equal to beerID (both).
The second one will return exactly beers which correspond to beerBrandID (excluding NULL beerIDs).
How about using with CASE statement in where clause like
WHERE
CASE WHEN beer.id IS NOT NULL
THEN beer.brand_id = 12345
ELSE TRUE
END
If you want to include records where there's no match, you need an outer join
SELECT beer.id AS beerID,
beer.barrelID AS barrelID,
beer.imageID AS imageID,
beer.title AS title,
beer.map AS map,
beer.longitude AS longitude,
beer.latitude AS latitude,
brand.name AS brandName,
brew.type AS brewType,
image.name AS imageName,
variation.name AS variationName
FROM brand, brew, image, beer
LEFT OUTER JOIN variation ON variation.ID = beer.VariationID
WHERE beer.id = %s
AND md5(beer.memberID) = %s
AND beer.review = 1
AND brand.ID = beer.brandID
AND brew.ID = beer.brewID
AND image.ID = beer.imageID
To check for null / not null, use IS NULL / IS NOT NULL
AND beerID IS NOT NULL
You can use "IS NULL" or "IS NOT NULL" MySQL comparison functions.
Read more about this here:
http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_is-null

Why doesn't this Rails SQL query work: companies.status = 'active' AND (companies.status_override = '' OR companies.status_override = NULL)

I have the following full query, and it returns nothing.
#companies.where("(companies.status = 'active' AND (companies.status_override = '' OR companies.status_override = NULL)) OR companies.status_override = 'active'")
I expect that the first part (companies.status = 'active' AND (companies.status_override = '' OR companies.status_override = NULL)) would return all items that have status set to 'active' and status_override set to nothing.
Without knowing the DB structure, the = NULL is a suspect. Try IS NULL instead.
Arithmetic comparison with NULL doesn't behave as you might expect; more details can be found in the Mysql Reference
The most likely explanation is that there aren't any rows that satisfy the predicates in the WHERE clause. Your where clause is of the form:
( a AND ( b OR c ) ) OR d
where
a = companies.status = 'active'
b = companies.status_override = ''
c = companies.status_override = NULL
d = companies.status_override = 'active'
So, basically, we know that there aren't any rows in the table that satisfy condition d.
And we know there aren't any rows that satisfy both conditions a and b.
Condition c is impossible; it will never be satisfied.
To test whether an expression contains a NULL, use expr IS NULL. i.e.
companies.status_override IS NULL
If you aren't concerned with an index range scan operation on the status_override column (it's likely that you aren't) you can simplify a bit using the IFNULL() function...
(companies.status = 'active' AND IFNULL(companies.status_override,'') = '') OR companies.status_override = 'active'
or using the NULLIF() function
(companies.status = 'active' AND NULLIF(companies.status_override,'') IS NULL) OR companies.status_override = 'active'

How to set If condition in Where clause in Sql

I want to display a criteria having multiple conditions, but if any condition is null or not having any value than how to set in where clause.
Example:
where
SR.fk_Designation_ID = #pk_Designation_ID
and SR.fk_SeniorityCategory_ID = #pk_SeniorityCategory_ID
and SR.IsCurrent_Appointment = 1 and SR.fk_SeparationType_ID is null
order by Senority_Joining_For_Order,E.Date_Of_Birth
here if #pk_SeniorityCategory_ID is null than I don't want to include the and SR.fk_SeniorityCategory_ID = #pk_SeniorityCategory_ID condition in where clause.
Try this:
WHERE
SR.fk_Designation_ID = #pk_Designation_ID
AND (#pk_SeniorityCategory_ID IS NULL
OR
SR.fk_SeniorityCategory_ID = #pk_SeniorityCategory_ID)
AND SR.IsCurrent_Appointment = 1 AND SR.fk_SeparationType_ID IS NULL
ORDER BY Senority_Joining_For_Order,E.Date_Of_Birth