mySQL query for empty and NULL value together - mysql

In a database, among the fields, I have two fields which are picName and UserName.
The picName can have a NULL value, a picture's name or it can be empty.
I want to query the userName of those entries whose picName is either NULL or empty.
I tried these commands separately
Select userName from user where picName = ''
Select userName from user where picName IS NULL
But I want to get both the results in 1 query.
I tried using IN operator like this
Select userName from user where picName IN ('' , IS NULL)
But it didn't work.
How should I do it...?

use OR if you have multiple filters in your condition
Select userName
from user
where picName IS NULL OR
picName = ''

An alternative to the other answers is to use MySQL's IFNULL() or COALESCE() functions:
SELECT userName FROM user WHERE IFNULL(picName='',TRUE);
SELECT userName FROM user WHERE COALESCE(picName,'') = '';

You want one of both conditions to be satisfied, so condition one OR condition two must be true, and that's exactly what you need to write:
Select userName from user where picName = '' OR picName IS NULL

SELECT userName FROM `user` WHERE picName IS NULL OR picName = ''

...ISNULL(picName ,'') is a function of MSSQL
select case when (picname is null or
picname = '' then username else 'filled' end
from user
gives you the UserName if picname = '' or null else the text 'Filled'.
If you want to filter these ones use (like the other said):
...where picname is null or picname = ''

Solution that checks for both empty and NULL values.
COALESCE(NULLIF(table.column, ''), table.fallback)
So your query will be:
SELECT userName FROM user WHERE NULLIF(picName, '') IS NULL

select userName from user WHERE ISNULL(picName ,'') = ''

Related

MySQL Filter Where if Column= NULL or Value

I am using Python with MYSql string queries and trying to do something like
"SELECT id from Example
WHERE item_column = (%s)"
Where in my case the passed in value can be a number or NULL. I understand that in SQL NULL represents nothing so this equality will not work. Normally, I would have to do something like column IS NULL. However, how do I set up my statement to account for values that can be NULL or not NULL?
Thank you
You can use the NULL-safe equal operator <=>.
"SELECT id from Example WHERE item_column <=> (%s)"
If you want give a default value for your variable incase its null you can use coalese(say 'xx' in case of string and -99.99 in case of integer)
"SELECT id from Example
WHERE item_column = COALESCE((%s),-99.99)"
But if you want to modify your statement based on the whether variable is null or not then you have to do a if else in the code
if variable is None:
return "SELECT id from Example WHERE item_column is null"
else:
return "SELECT id from Example WHERE item_column = variable"

Case Statement with comparison

In the existing statement below I am comparing the host_name and ip_adress with records retrieved from an other table and mark the column as true when found, but I would like to tweak it to give a true result when the host_name is also a part of identifer from the record retrieved
i.e
host_name=abc
identifier=abc.google.com should also be true
CASE
WHEN
(
(`inv_y`.`host_name` <> '')
AND
`inv_y`.`host_name` IN (SELECT `inv_x`.`identifier`
FROM `inv_x`
ORDER BY `inv_x`.`creation_date` DESC
)
)
OR
(
(`inv_y`.`ip_address` <> '')
AND
inv_y`.`ip_address` IN (SELECT `inv_x`.`identifier`
FROM `inv_x`
ORDER BY `inv_x`.`creation_date` DESC
)
)
THEN
'True'
ELSE
'False'
END
It looks like you could benefit from the EXISTS function. Something like the following could work:
SELECT CASE WHEN EXISTS(SELECT 1
FROM inv_y
WHERE (ip IN (SELECT identifier
FROM inv_x)
OR hostname IN (SELECT identifier
FROM inv_x))
AND hostname LIKE '%eb%'
) = 1 THEN "True"
ELSE "False"
END AS Present;
Here's a demo of this working: SQL Fiddle
Note, the above could be simplified further if you can work with a 1 or 0 instead of true or false. EXISTS returns a 1 if the row is found, and 0 if not, and the CASE is there simply to translate this into 'True' or 'False', so you could simply have the following:
SELECT EXISTS(SELECT 1
FROM inv_y
WHERE (ip IN (SELECT identifier
FROM inv_x)
OR hostname IN (SELECT identifier
FROM inv_x)
)
AND hostname LIKE '%eb%') AS Present;

Mysql: If a field in a record is empty I want the stored procedure to return N/A as value

I have a table in MySQL where in a record a field (Default: None) does not have any value.
I'm using a stored procedure to select values from this table and I when this field has no value I should get the value N/A.
I tried the following code but I get the field with no value.
SELECT md.coding_id,
md.patient_id,
md.implant_date,
(case
when md.device_and_implant_description = ''
then 'N/A'
when md.device_and_implant_description !=0
then md.device_and_implant_description
end) as device_and_implant_description
FROM medical_devices_mapping as md
WHERE md.patient_id = p_id
The p_id value is given by the user.
This is the result:
This is the structure of my table:
Please use NUll as well as "".
If you want to use NULL or '' only then before storing data, make sure that you are storing null or empty string then accordingly use condition.
Use REGEXP condition instead of ''
SELECT md.coding_id, md.patient_id, md.implant_date,
CASE WHEN md.device_and_implant_description REGEXP '^[A-Za-z0-9]+$'
THEN md.device_and_implant_description
ELSE 'N/A'
END AS device_and_implant_description,
md.device_and_implant_description
FROM medical_devices_mapping md
WHERE md.patient_id = p_id
Maybe you should invert your logic
case when trim(md.device_and_implant_description) is not null then md.device_and_implant_description
else 'N/A'
end
I found the solution using COALESCE().
The proposed solution is the following:
SELECT md.coding_id,
md.patient_id,
md.implant_date,
(CASE when COALESCE(md.device_and_implant_description, '') !=''
then md.device_and_implant_description
when COALESCE(md.device_and_implant_description, '') =''
then 'N/A'
end) as device_and_implant_description
FROM medical_devices_mapping as md
WHERE md.patient_id = p_id

Is it possible to select specific values based on some condition?

I'm wondering if it's possible to select 'true' or 'false' from myTable based on if the given row matches some criteria. So, for example if a user does or doesn't have a value in a column called phone_number I'd do...
select (**some magic here**) as has_phone_number from users;
and the result would be a bunch of 'true' or 'false'
TIA.
IF (phone_number != '', true, false) as has_phone_number
or even
phone_number != '' as has_phone_number
Use a case statement.
SELECT CASE WHEN Phone IS NULL THEN FALSE ELSE TRUE END AS has_phone_number FROM users;
select
phone = '' as has_phone_number
from users

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