WHERE Clause only IF NOT NULL - mysql

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

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.

Select null or not null depend on parameter

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

NULL != NULL in mysql query

I am trying to do a query that sees if fields are equivalent. However, whenever the field is NULL it returns a false result. I even tried doing the same thing with the column itself:
SELECT * FROM `mturk_completion` WHERE (`mturk_completion`.`imdb_url` =
`mturk_completion`.`imdb_url` AND `mturk_completion`.`worker_id` = 'A3NF84Q37D7F35' )
And it only returns results where the column is not NULL. Why is this so, and how do I get around it?
Your title is absolutely correct for any SQL implementation (not just MySQL). NULL is not equal to anything (including another NULL).
You need to use explicit IS NULL check or COALESCE() function (or its RDBMS-dependent alternatives) to set some default value in case of NULL.
Your comparison of mturk_completion.imdb_url to itself is redundant and should always return True, except when mturk_completion.imdb_urlis Null, in which case it will return Null.
That's because the operator = returns either True, False when comparisons can be made or Null, when either of the two operators is Null
Try this to illustrate the situation.
SELECT 1 = NULL; -- returns NULL
SELECT 1 != NULL; -- also return NULL
SELECT ISNULL(1 = NULL); -- returns 1
SELECT ISNULL(1 != NULL); -- returns 1
If you rewrite your query like below, your problems with ignoring NULLs will go away:
SELECT * FROM `mturk_completion` WHERE worker_id = 'A3NF84Q37D7F35'
I think you can use
(table.Field = table2.Field OR COALESCE(table.Field, table2.Field) IS NULL)

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.)

MySQL Left Join, SELECT a field either from one or the other if null

I am trying to LEFT JOIN 2 tables. which is working out fine. But i am getting back two sets of fields named setting_value. iam trying to get tblSettings.setting_value only if tblAgencySettings.setting_value is NULL. How would i go about doing this? I know i can rename the fields, then in PHP i can check the tblAgencySettings.setting_value and if NULL then grab the tblSettings.setting_value but i prefer to keep this at MySQL.
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
`tblSettings`.`setting_value`, `tblAgencySettings`.`setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
slight issue i just noticed. i failed to mention this. if tblAgencySettings.setting_value does have a value. but changeable is not 1 then just select tblSettings.setting_value
Just add a COALESCE:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
The COALESCE function returns the first non-NULL value you give it so this:
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
Will be tblAgencySettings.setting_value if that's not NULL and tblSettings.setting_value if tblAgencySettings.setting_value is NULL.
If tblAgencySettings.setting_value can also be zero and you want to ignore that as well as NULL, then you could use this instead of the COALESCE above:
COALESCE(
IF(`tblAgencySettings`.`setting_value` = 0, NULL, `tblAgencySettings`.`setting_value`),
`tblSettings`.`setting_value`
)
The IF returns the second argument if the first is true and the third if the first argument is false so the above use converts zero to NULL. Or, you could go all the way to a CASE statement:
case
when `tblAgencySettings`.`setting_value` = 0 then `tblSettings`.`setting_value`
when `tblAgencySettings`.`setting_value` IS NULL then `tblSettings`.`setting_value`
else `tblSettings`.`setting_value`
end
Change your SQL Statement to this:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
CASE WHEN `tblSettings`.`setting_value` IS NULL THEN `tblAgencySettings`.`setting_value`
ELSE `tblSettings`.`setting_value` END AS `setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
Here's a link to MYSQL CASE Statement for your reference.