mysql-update multiple columns in a single row with multiple where clauses - mysql

I have a mysql table as follows:
name | serialno | key
---------------------
NULL | NULL | 10
luke | 1234 | 20
NULL | NULL | 30
NULL | NULL | 40
I have to update a row where the name and the serialno update for a valid key.
The update statements that I am running are as follows:
UPDATE test SET Name = 'pc', Serialno = '10', WHERE `Key` = '10' AND name is null
and
UPDATE test SET Name = CASE
WHEN NULL THEN 'pc'
END
SET Serialno = CASE
WHEN NULL THEN '10'
END
Where `key` = '20'
The following errors get displayed:
1.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Key = '10' AND name is null' at line 1:
2.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET Serial = CASE
WHEN NULL THEN '10'
END
Where `key`='20'' at line 4:
Please help.

UPDATE test SET Name = CASE Name
WHEN NULL THEN 'pc'
END,
SET Serialno = CASE Serialno
WHEN NULL THEN '10'
END
Where `key` = '20'
You have to specify condition of your CASE

I don't really know what is the context (can you explain the detail what you want and then I will help you with better result).
However, as the StanislavL's answer, the syntax was wrong. I corrected it as below:
UPDATE test SET Name = CASE Name
WHEN NULL THEN 'pc'
END,
Serialno = CASE Serialno
WHEN NULL THEN '10'
END
Where `key` = '20'

Please do like this
UPDATE test SET Name = 'pc', Serialno = '10' WHERE `Key` = '10' AND name is null

Reasons for Errors:
Error 1 was because you have a , comma before WHERE Key = '10' ....
Statement should be:
UPDATE test SET Name = 'pc', Serialno = '10' WHERE `Key` = '10' AND name is null
Error 2 was because you have not instructed which case value to check in when clause. Acceptable syntaxes are:
1. CASE column_name WHEN value THEN expr1 ELSE expr2 END
2. CASE WHEN column_name <condition> value THEN expr1 ELSE expr2 END
Statement should be:
UPDATE test
SET
Name = CASE WHEN name IS NULL THEN 'pc' else name end
, Serialno = CASE WHEN serialno IS NULL THEN '10' else serialno end
END
Where `key` = '10'
Example # SQL Fiddle
You can extend this to set values for columns based on different key values without using where clause.
Example:
UPDATE test
SET
Name = CASE WHEN `key`=10 and name IS NULL THEN 'pc' else name end
, Serialno = CASE WHEN `key`=10 and serialno IS NULL THEN '10' else serialno end
, Name = CASE WHEN `key`=30 and name IS NULL THEN 'key is 30' else name end
, Serialno = CASE WHEN `key`=30 and serialno IS NULL THEN '3' else serialno end
END
Refer to: MySQL: CASE - WHEN syntax

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]

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

Write IF..ELSE in MYSQL

I have a table looked like this
mysql> select * from rc;
+----+----------------------------+----------------------------+----------------------------+
| id | business_date | cutoff_dt | recon_dt |
+----+----------------------------+----------------------------+----------------------------+
| 1 | 2017-03-21 16:50:29.032000 | 2017-03-21 16:50:31.441000 | 2017-03-21 16:50:32.832000 |
+----+----------------------------+----------------------------+----------------------------+
1 row in set (0.00 sec)
I want to run query
-Select * from rc where business_date = '2017-03-17'
- ifcutoff_dt` is null or empty, it will display null, otherwise display not null
I wrote it into shell script.
#! /bin/bash
mysql -u root -p <<rcard
use rcard;
SELECT *
(CASE WHEN (cut_off_dt = "NULL")
THEN
echo "Null"
ELSE
echo "NOT NULL"
END)
from rc WHERE business_date = '2017-03-17';
rcard
But I get error
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(CASE WHEN (cut_off_dt = "NULL")
THEN
echo "Null"
ELSE
' at line 2
Is it the correct way to write the IF ELSE in MySQL ?
This is not the correct way to check for NULL values.
Try this:
SELECT *,
IF(mepslog_cut_off_dt IS NULL, 'NULL', 'NOT NULL')
from rc_mepslog
WHERE mepslog_business_date = '2017-03-17';
You can alternatively use a CASE expression:
SELECT *,
CASE
WHEN mepslog_cut_off_dt IS NULL THEN 'NULL'
ELSE 'NOT NULL'
END
from rc_mepslog
WHERE mepslog_business_date = '2017-03-17';
SELECT *, (CASE WHEN (mepslog_cut_off_dt IS NULL)
THEN
'Null'
ELSE
'NOT NULL' END) from rc WHERE business_date = '2017-03-17'
To compare if the value is null, use column IS NULL instead of column = 'NULL'; the latter would check if the column you are comparing has the string 'null'
There's a missing comma after select * and none of your table names you specified in the query exists. To separate a table from the column, you use a dot .. There are other wrong written column names appart from that. Also, you don't use echo, you simply specify what to do. Also, I'd suggest to use an alias for the selected column from the case (AS 'nullOrNotNull')
SELECT * ,
CASE WHEN cut_off_dt IS NULL
THEN
'Null'
ELSE
'NOT NULL'
END AS 'nullOrNotNull'
from rc WHERE business_date = '2017-03-17';

Mysql case statement not returning appropriate message after case evaluation

I am selecting engineStatus field from car table, the query looks like so :-
select engineStatus from car LIMIT 1
car table column can have values 1 or 0.
Assuming engineStatus is set to 1, the above query will return 1.
I want to return ON if engineStatus = 1 or return OFF when engineStatus = 0
I've tried below query, but SQL throws an Error
SELECT ( case engineStatus = 1 then SET engineStatus = 'ON' else SET status = 'OFF') FROM `car` WHERE 1
Mysql Says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'then SET engineStatus = 'ON' else SET engineStatus = 'OFF') FROM `car` WHERE 1 LI' at line 1
How can i achieve this in mysql ?
You miss WHEN and there's no need SET here
The proper syntax is :
SELECT
( CASE
WHEN engineStatus = 1 THEN 'ON'
ELSE 'OFF'
END
) AS status
FROM `car`
WHERE 1
It should be:
SELECT ( case WHEN engineStatus = 1 then 'ON' else
'OFF' END) FROM `car`;