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

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

Related

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

How to select last column without Null in mySQL

I am facing an issue I cannot seem to find the solution of anywhere. I have a table in mySQL with following columns:
RoleDuty1
RoleDuty2
RoleDuty3
RoleDuty4
RoleDuty5
RoleDuty6
RoleDuty7
This table has just one row or record. I want to see which is the last column in the table which is Not NULL? meaning that I want to dynamically get Column Name RoleDuty7 as an answer from mySQL, since all of the columns have some value other than Null inside them.
Anyone has any idea how can I do this?
This is exactly what the coalesce function is for:
SELECT COALESCE(RoleDuty1,RoleDuty2,RoleDuty3,RoleDuty4,RoleDuty5,RoleDuty6,RoleDuty7,'All null) as FIRST_NON_NULL
FROM aTable
I think you have to use a COALESCE with a bunch of CASE statements, like this:
SELECT COALESCE(rd1, rd2, rd3, rd4, rd5, rd6, rd7)
FROM
(SELECT
CASE WHEN roleduty1 IS NOT NULL THEN 'roleduty1' ELSE NULL END AS rd1,
CASE WHEN roleduty2 IS NOT NULL THEN 'roleduty2' ELSE NULL END AS rd2,
....
FROM t) sub
SELECT (
CASE FirstNullColumn
WHEN 'RoleDuty1' THEN 'None'
WHEN 'RoleDuty2' THEN 'RoleDuty1'
WHEN 'RoleDuty3' THEN 'RoleDuty2'
WHEN 'RoleDuty4' THEN 'RoleDuty3'
WHEN 'RoleDuty5' THEN 'RoleDuty4'
WHEN 'RoleDuty6' THEN 'RoleDuty5'
WHEN 'RoleDuty7' THEN 'RoleDuty6'
ELSE 'RoleDuty7'
END
) AS LastNonNullColumn
FROM (
SELECT (
CASE
WHEN RoleDuty1 IS NULL THEN 'RoleDuty1'
WHEN RoleDuty2 IS NULL THEN 'RoleDuty2'
WHEN RoleDuty3 IS NULL THEN 'RoleDuty3'
WHEN RoleDuty4 IS NULL THEN 'RoleDuty4'
WHEN RoleDuty5 IS NULL THEN 'RoleDuty5'
WHEN RoleDuty6 IS NULL THEN 'RoleDuty6'
WHEN RoleDuty7 IS NULL THEN 'RoleDuty7'
ELSE 'None'
END
) AS FirstNullColumn
FROM MyTable
)

SELECT STATEMENT IN A CASE STEMENT INSIDE A SELECT STATEMENT

I am trying to display a field value based on the value of field and then find a external table record.
can I do it?
SELECT
CASE
WHEN (dsp_notes IS NOT NULL) THEN '*'
WHEN (dsp_notes IS NULL) THEN ''
ELSE ''
END,
CASE
WHEN (dsp_priority = '1') THEN [SELECT uvi_value FROM PUB.universalinfo WHERE uvi_key = 'DSP01SHORT']
Is this possible?
Yes. This is called a scalar subquery and it needs to return one column and one row:
(CASE WHEN dsp_priority = '1'
THEN (SELECT ui.uvi_value FROM PUB.universalinfo ui WHERE ui.uvi_key = 'DSP01SHORT')
END) as NewCol
I strongly encourage you to use table aliases on your column references.

mySQL query for empty and NULL value together

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 ,'') = ''