MYSQL IF value = null OR value = "" - mysql

I have this part of query:
IF(orders = NULL OR orders = '', "value1', 'value2')
which works with empty cells but not with null ones, any help?
When it's NULL it doesn't make anything but when it's '' it runs the query

It's spelled orders is NULL (not orders = NULL).

You have to use
IF(orders IS NULL OR orders = '', 'value1', 'value2')
instead

Related

Difference between (NULL) and empty in mysql column values

I am working in a company where we use Spring -Hibernate and mysql database at backend.
There is a table tc_wallet
In this table i have an column tips_type which has values
Collection,Payable
'' (empty)
NULL --> No value has been initialized
Now when i fire a query:
SELECT *
FROM `tc_wallet`
WHERE
login_id = 'gaurav.wakharkar'
AND `delete_flag` = 'F'
AND `tips_type` != 'Collection'
I get results which has column value as '' (empty).
Login_id tips_type
gaurav.wakharkar
gaurav.wakharkar
gaurav.wakharkar
But even (NULL) is != 'Collection' should satisfy the above condition.
So according to me the result should have been .
Login_id tips_type
gaurav.wakharkar
gaurav.wakharkar
gaurav.wakharkar
gaurav.wakharkar (NULL)
gaurav.wakharkar (NULL)
Is there some issue while checking/comparing values with (NULL) ?
Does it behave differently ?
To check for nullness, you want to use IS NULL. Comparing NULL to something else with the equality operator (or the inequality operator) is always false.
Consider:
SELECT *
FROM `tc_wallet`
WHERE
login_id = 'gaurav.wakharkar'
AND `delete_flag` = 'F'
AND (`tips_type` IS NULL OR `tips_type` != 'Collection')
change your query to
SELECT
*
FROM
`tc_wallet`
WHERE login_id = 'gaurav.wakharkar'
AND `delete_flag` = 'F'
AND (`tips_type` != 'Collection' or `tips_type` is null)

Disable NULL in CASE WHEN condition

I tried to run the script below, and it will display NULL value. I only want to get data for ALBANIA without having NULL value.
SELECT [COUNTRY_CODE_NEW]
,CASE WHEN SUBSTRING([COUNTRY_CODE_NEW], 6,2) = '16' THEN 'ALBANIA'
END 'COUNTRY'
FROM [dbo].[COUNTRY_NEW]
Can you do:
CASE WHEN SUBSTRING([COUNTRY_CODE_NEW], 6,2) = '16' AND IS NOT NULL THEN 'ALBANIA'
Just add a check to ensure it's not null:
SELECT
[COUNTRY_CODE_NEW],
CASE WHEN SUBSTRING([COUNTRY_CODE_NEW], 6,2) = '16' AND [COUNTRY_CODE_NEW] IS NOT NULL THEN 'ALBANIA'
END 'COUNTRY'
FROM [dbo].[COUNTRY_NEW]

Creating a MySQL view due to NULL Values

I try to setup the following VIEW and defined 3 cases that lead to a specified match between columns of 2 tables in the WHERE clause.
This works more or less, because the view has entries that should have been excluded due to my statement. Please see the screenshot with the select * output therefore.
Can you help me here? Many thanks beforehand.
CREATE OR REPLACE VIEW `match` AS SELECT
a.`werksnr`,
b.`werksnr` AS `werksnr_ref`,
a.`EAN`,
b.`EAN` AS `EAN_ref`,
a.`lieferant`,
b.`lieferant` AS `lieferant_ref`,
a.`artikel`,
b.`artikel` AS `artikel_ref`,
a.`artikelbezeichnung` AS `bezeichnung_suspect`,
b.`artikelbezeichnung1`,
b.`artikelbezeichnung2`
FROM suspect as a, reference as b WHERE
(a.EAN IS NOT NULL AND b.EAN IS NOT NULL AND a.werksnr IS NOT NULL and b.werksnr IS NOT NULL AND a.EAN = b.EAN AND a.werksnr = b.werksnr) OR
(a.lieferant IS NOT NULL AND b.lieferant IS NOT NULL AND a.werksnr IS NOT NULL AND b.werksnr IS NOT NULL AND a.lieferant = b.lieferant AND a.werksnr = b.werksnr) OR
(a.lieferant IS NOT NULL AND b.lieferant IS NOT NULL AND a.EAN IS NOT NULL AND b.EAN IS NOT NULL AND a.lieferant = b.lieferant AND a.EAN = b.EAN);
screenshot

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

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.