Creating a MySQL view due to NULL Values - mysql

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

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)

MYSQL IF value = null OR value = ""

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

MYSQL: enum field searching with NULL values

table person:
name
type enum('admin','user','random') NULL DEFAULT NULL
When this query is performed, it doesnt return the records that have a type of NULL
select * from person where type != 'admin';
null cannot be tested for with (in)equality statements. you need to use IS NULL. e.g.
select *
from person
where (type != 'admin') or (type IS NULL)
e.g. null is "contagious"
null > x -> null
null = x -> null
null = null -> null
null + 1 -> null
null * 1 -> null
etc... it's basically "unknown". Mixing known and unknown in sql makes the result unknown, always. Hence the special ifnull(), coalesce(), and "if null" tests/functions.
You can use the NULL safe equality operator in MySQL
select *
from person
where not type <=> 'admin'

MYSQL Using UPDATE, WHEN THEN with a NULL value

I'm trying to figure out why I'm not getting a result from a MySQL Query I'm running.
I'm trying to replace a NULL value with a number with in a query, but I can't figure out what I'm doing wrong.
Here's my query:
UPDATE Details
SET HowHear_ID = CASE HowHear_ID
WHEN '' THEN 25
WHEN NULL THEN 25
WHEN 7 THEN 25
WHEN 8 THEN 5
WHEN 16 THEN 25
WHEN 17 THEN 16
END
WHERE HowHear_ID IN ('',NULL,7,8,16,17)
This Query will effect all but the NULL values.
What am I doing wrong??
No value will ever equal (or "unequal") NULL in SQL. Understand the following truth table:
NULL = NULL yields NULL -- not FALSE!
NULL != NULL yields NULL -- not TRUE!
[ANY] = NULL yields NULL -- not FALSE!
[ANY] != NULL yields NULL -- not TRUE!
Since the following are equivalent...
[expression] IN (a, b, c)
[expression] = ANY (a, b, c)
[expression] = a OR [expression] = b OR [expression] = c
... you cannot put NULL on the right hand side of an IN predicate. Interestingly, things get even worse when you put NULL on the right hand side of a NOT IN predicate:
[expression] NOT IN (a, b, c)
[expression] != ANY (a, b, c)
[expression] != a AND [expression] != b AND [expression] != c
If b were NULL, the whole expression will become NULL (or maybe FALSE), but never TRUE. This is also the case for NOT IN (subselect) predicates! So, never do this:
[expression] NOT IN (NULL, 1, 2)
The correct solution in your case uses a NULL predicate instead. Do this:
UPDATE Details
SET HowHear_ID = CASE
WHEN HowHear_ID = '' THEN 25
WHEN HowHear_ID IS NULL THEN 25 -- Use a NULL predicate here
WHEN HowHear_ID = 7 THEN 25
WHEN HowHear_ID = 8 THEN 5
WHEN HowHear_ID = 16 THEN 25
WHEN HowHear_ID = 17 THEN 16
END
WHERE HowHear_ID IN ('',7,8,16,17)
OR HowHear_ID IS NULL -- Use a NULL predicate here
Or this:
WHERE COALESCE(HowHear_ID, '') IN ('',7,8,16,17)
You can't reference NULL in a WHERE clause and get the results you expect. NULL behaves differently to other values.
If you need to reference it, you need to use the isnull() function.
in your case, you would write something like this:
WHERE HowHear_ID IN ('',7,8,16,17) or isnull(HowHear_ID)
By the way, you haven't specified the data type of the field. I assume it's an integer though. In that case, it might be better to check for zero rather than an empty string? (if it isn't an integer, then perhaps it should be)
You can achieve that with
WHERE HowHear_ID IN ('',7,8,16,17) OR HowHear_ID IS NULL

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