Write IF..ELSE in MYSQL - 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';

Related

How to implement "SHOW COLUMN FROM (SELECT Result) WHERE Column_name = 'column_name'"?

Good Evning My Brothers/Sisters.
I want to check whether a column is exist from select result.
I have tried some effort like below but instead it gives me an error.
SHOW COLUMNS
FROM (SELECT
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS sudah_isi, SUM(hasil.belum_isi) AS belum_isi
FROM
(
SELECT
a.id_pegawai, c.skpd, c.kode_skpd, a.id_satuan_organisasi, a.nama_pegawai, a.nip,
CASE
WHEN i.id_pegawai IS NULL THEN 0
ELSE 1
END AS sudah_isi,
CASE
WHEN i.id_pegawai IS NULL THEN 1
ELSE 0
END AS belum_isi
FROM
tbl_pegawai a
LEFT JOIN ref_skpd c ON a.id_satuan_organisasi = c.id_skpd
LEFT JOIN tbl_bapertarum i ON (a.id_pegawai = i.id_pegawai AND YEAR(i.tgl_lapor)='2015')
GROUP BY
a.id_pegawai
) hasil
WHERE
1 AND hasil.kode_skpd LIKE 'Badan Kepegawaian Daerah%'
GROUP BY
hasil.id_satuan_organisasi
ORDER BY
hasil.kode_skpd) WHERE Field = "kode_skpd"
This is the full error result:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(SELECT
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS s' at line 2
How do i to find the existence of a column or columns from SELECT result ?
Thanks in advance Brothers/Sisters.

phpmyadmin where condition in mysql union got sintax error

I have tried that in phpmyadmin (4.5.2) and work OK:
(SELECT 'db1' as DB_NAME, db1_col1, null as dbd2_col1 FROM db1.db1_table1)
UNION
(SELECT 'db2' as DB_NAME, null as dbd1_col1, db2_col1 FROM db2.db2_table1)
The result is:
DB_NAME db1_col1 dbd2_col1
db1 DB1_col1_val1 NULL
db2 NULL DB2_col1_val1
But if i try to add a where clause I got sintax error:
(SELECT 'db1' as DB_NAME, db1_col1, null as dbd2_col1
FROM db1.db1_table1
WHERE db1_col1 = 'DB1_col1_val1'
)
UNION
(SELECT 'db2' as DB_NAME, null as dbd1_col1, db2_col1
FROM db2.db2_table1
WHERE db2_col1 = 'DB1_col1_val1'
)
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 'UNION
(' at line 4
This made me crazy!
Nevertheless, when I tried it on console, it worked as espected:
mysql> (SELECT 'db1' as DB_NAME, db1_col1, null as dbd2_col1
-> FROM db1.db1_table1
-> WHERE db1_col1 = 'DB1_col1_val1'
-> )
-> UNION
-> (SELECT 'db2' as DB_NAME, null as dbd1_col1, db2_col1
-> FROM db2.db2_table1
-> WHERE db2_col1 = 'DB1_col1_val1'
-> );
+---------+---------------+-----------+
| DB_NAME | db1_col1 | dbd2_col1 |
+---------+---------------+-----------+
| db1 | DB1_col1_val1 | NULL |
+---------+---------------+-----------+
1 row in set (0.00 sec)
And the question:
Is this a bug of phpmyadmin or am I missing something else?
Since it worked in the console, it is probably a phpmyadmin bug. My guess is that phpmyadmin tried to insert its limit clause after the closing parentheses of the 1st query.
UPDATE
Apparently, this is the same bug as fixed here. If you upgrade to phpmyadmin v4.5.3.0 or later, then this bug is no longer there.
Your use of phpmyadmin documentation indicates you forgot the ; at the end of your request. It was present on the console documentation. Just a thought.

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`;

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

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

returning a yes or no value based on 2 matching fields

I had a Previous Question about setting a value to yes or no based on fields from 2 table matching. I am trying to have the query check the generalsource.firstinventor and if it matches inventor.inventorfull insert "Yes" or else insert "No"
The result was the following code:
UPDATE i
SET LeadInventor =
CASE
WHEN fi.FirstInventorName IS NULL THEN 'No'
ELSE 'Yes'
END
FROM Inventor i
LEFT JOIN FirstInventor fi
ON i.InventorFirst + ' ' + i.InventorLast = fi.FirstInventorName
Which worked fantastically, until I tried to put into MySQL and I can not figure out whats wrong with the syntax. How do I fix it?
Error Code: 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 'FROM Inventor i LEFT JOIN FirstInventor fi ON i.InventorFirst + ' ' + i.' at line 7
CONCAT string as
CONCAT( i.InventorFirst, ' ', i.InventorLast )
UPDATE: Also, there seems to be syntactic problem in your update statement, it should be as follows
UPDATE
Inventor i
LEFT JOIN FirstInventor fi ON CONCAT( i.InventorFirst, ' ', i.InventorLast ) = fi.FirstInventorName
SET LeadInventor =
CASE
WHEN fi.FirstInventorName IS NULL THEN 'No'
ELSE 'Yes'
END
More info: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat