Why mysql case condition not working correctly - mysql

I have the following code
set #string:='11';
select CASE #string
WHEN CAST(#a AS SIGNED)=11 THEN 'yes'
ELSE 'no'
END as filed;
I don't understand why it returns 'no'.
And also if I use WHEN #string='11' THEN 'yes' it also returns no.

Is this what you were aiming for?
set #string:='11';
select CASE WHEN CAST(#string AS SIGNED)='11' THEN 'yes' ELSE 'no' END as filed;
Can also be expressed as
set #string:='11';
select CASE CAST(#string AS SIGNED) when '11' THEN 'yes' ELSE 'no' END as filed;

set #string:='11';
select CASE WHEN CAST(#string AS SIGNED) = 11 THEN 'yes'
ELSE 'no'
END as filed;
Demo

Because you have not defined #a, so the value is NULL.
Hence, the ELSE clause is always going to be returned.
Then your second problem is that this does not work:
set #a = '11';
SELECT CASE #a
WHEN CAST(#a AS SIGNED) = 11 THEN 'yes'
ELSE 'no'
END as filed;
This is because CAST(#a AS SIGNED) = 11 is a boolean expression whose value is 0, 1 or NULL and that is never 11.
These do work:
SELECT CASE WHEN CAST(#a AS SIGNED) = 11 THEN 'yes'
ELSE 'no'
END as filed;
SELECT CASE #a WHEN 11 THEN 'yes'
ELSE 'no'
END as filed;

Related

MySql select statement with conditional where clause

I am trying to write a MySql statement with a conditional where clause.
something like this:
set #price = 5000 ;
set #city = 1324368075;
select count(*)
from property
where case when #price is not null then
price < #price
end
and (case when #city is not null then
CityId = #city
end)
the variable should be included in the query only if it is not null.
My attempts have failed so far. Any ideas?
Edited:
Sorry I spoke too soon ysth,
these two queries are supposed to yield the same count but they dont.
Edit #2: Execution plan & indexes
Here's the query:
set #CountryId = null ;
set #CityId = 1324368075 ;
set #StateProvince = null ;
set #CategoryId = null ;
set #TransactionTypeId = null;
set #Price = 5000;
SELECT
Count(*)
FROM
meerkat.property
WHERE
(CASE WHEN #CountryId IS NOT NULL THEN CountryId = #CountryId ELSE 1 END)
AND (CASE WHEN #CityId IS NOT NULL THEN CityId = #CityId ELSE 1 END)
AND (CASE WHEN #CategoryId IS NOT NULL THEN CategoryId = #CategoryId ELSE 1 END)
AND (CASE WHEN #StateProvince IS NOT NULL THEN StateProvince = #StateProvince ELSE 1 END)
AND (CASE WHEN #TransactionTypeId IS NOT NULL THEN TransactionTypeId = #TransactionTypeId ELSE 1 END)
AND (CASE WHEN #Price IS NOT NULL THEN Price <= #Price ELSE 1 END)
AND IsPublic = 1
AND IsBlocked = 0;
Thanks in advance
If no when conditions are met, case returns null. If you want each test to pass, you need to return a true value instead, so:
case when #price is not null then
price < #price
else 1 end
and ...

How to use Group By correclty?

I have this query that worked fine:
select isnull(email,'') as Email ,isnull([ERPM First Name],'')+' '+isnull([ERPM Last Name],'')[User Name],
geo,CustomerID,BusinessID,courseid, MIN (CompletionDate) [1st Training Course],
CASE WHEN COURSEID IN (37445,37644,37443,37778,37435,37733,37584,37483,37392,37817,
37259,37597,37391,37393,37792,37816,37256,37257,37258,37484,37485,37486)
THEN 'Yes' ELSE 'No'
END AS [Is it a campaing course?],
CASE WHEN CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN 'Yes' ELSE 'No'
END AS [During Campaign],
CASE WHEN COURSEID IN (37256,37257,37258,37484,37485,37486) AND
CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN 'ON Period Bonus' ELSE '-'
END AS [1st BONUS]
from vw_Training_Cube
where [Is disti or subdisti?] = 'No' and [Is test account?] = 'No'
and Email<>'0'
GROUP BY isnull(email,''),isnull([ERPM First Name],'')+' '+isnull([ERPM Last Name],''),geo,CustomerID,BusinessID,courseid,
CASE WHEN COURSEID IN (37445,37644,37443,37778,37435,37733,37584,37483,37392,
37817,37259,37597,37391,37393,37792,37816,37256,37257,37258,37484,37485,37486)
THEN 'Yes' ELSE 'No'
END,
CASE WHEN CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN 'Yes' ELSE 'No'
END,
CASE WHEN COURSEID IN (37256,37257,37258,37484,37485,37486) AND
CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN 'ON Period Bonus' ELSE '-5'
END
but now instead of grouping by email, I want to group by business id. But simply swapping the order doesnt solve the problem.
Unless you need an aggregate function such as COUNT() MIN() or MAX() then you can simplify your query by using select distinct.
SELECT DISTINCT
ISNULL(email, '')
AS Email
, ISNULL([ERPM First Name], '') + ' ' + ISNULL([ERPM Last Name], '')
[User Name]
, geo
, CustomerID
, BusinessID
, courseid
, MIN(CompletionDate) [1st Training Course]
, CASE
WHEN COURSEID IN (37445, 37644, 37443, 37778, 37435, 37733, 37584, 37483, 37392, 37817,
37259, 37597, 37391, 37393, 37792, 37816, 37256, 37257, 37258, 37484, 37485, 37486) THEN
'Yes'
ELSE
'No'
END AS [Is it a campaing course?]
, CASE
WHEN CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN
'Yes'
ELSE
'No'
END AS [During Campaign]
, CASE
WHEN COURSEID IN (37256, 37257, 37258, 37484, 37485, 37486) AND
CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN
'ON Period Bonus'
ELSE
'-'
END AS [1st BONUS]
FROM vw_Training_Cube
WHERE [Is disti or subdisti?] = 'No'
AND [Is test account?] = 'No'
AND Email <> '0'
ORDER BY BusinessID
To reduce the rows further, you also need to remove columns - OR - start using aggregate functions. e.g. the following would produce the minimum set of rows to list every BusinessID that meets the where conditions.
SELECT DISTINCT
BusinessID
FROM vw_Training_Cube
WHERE [Is disti or subdisti?] = 'No'
AND [Is test account?] = 'No'
AND Email <> '0'
ORDER BY BusinessID
;
Keep adding columns to that to see the effect on number of rows.

If else condition in mysql query

How can I use if and else condition in mysql query. Here is my query.
SELECT CASE WHEN IDParent < 1 THEN 'no' ELSE 'yes' END AS ColumnName FROM tableName;
Try this;) Take a look of mysql-if-function.
SELECT If(IDParent < 1, 'no', 'yes') AS ColumnName FROM tableName;

Case statement in sqlserver in where clause with else as always true

I have used the always true statement e.g. 1 = 1 in case statement of where clause in MYSQL with following syntax:
select * from tablename where
(case when tablefield is not null then
then tablefield = 'value'
else 1 = 1 end)
I want to know how can i use else 1 = 1 (always true statement) in sqlserver/tsql case statement in where clause.
you would not use case you would just write a multi conditional statement. In your case it would look like
Where (tablefield = 'value'
OR tablefield is null)
If you want to use the TSQL CASE function, you could do something like :
select * from tablename where 1 =
(case when tablefield is not null then
(case when tablefield = 'value' then 1 else 0 end)
else 1 end)
which could be simplified to :
select * from tablename where 1 =
(case when tablefield is null then 1 when tablefield = 'value' then 1
else 0 end)
You can leave out the "else 0" parts, as when no match is found, NULL is returned (which will not be equal to 1). i.e.:
select * from tablename where 1 =
(case when tablefield is not null then
(case when tablefield = 'value' then 1 end)
else 1 end)
select * from tablename where 1 =
(case when tablefield is null then 1 when tablefield = 'value' then 1 end)
Sorry I just mixed up the query,I was intended to ask for a condition when parameter variable is null instead of tablefield
In that case query may look like this :
select * from tablename where
(case when parameterfield_or_variable is not null then
then tablefield = 'value'
else 1 = 1 end)
or when using parameterfield for value
(case when parameterfield_or_variable is not null then
then tablefield = parameter_field_or_variable
else 1 = 1 end)
The answer as per asked question #KHeaney is right.
However the query in tsql/sqlserver as described in mysql, will be like this :
select * from tablename
where tablefield = #parameterfield
-- incase of comparing input parameter field with tablefield
or #parameter is null
so when #parameterfield will be null it will show all results otherwise it will restrict to only input value.
Thanks
You can try...
select * from tablename
where tablefield = isnull(#parameter, tablefield)
I know T-SQL and MySQL both provide COALESCE, which returns the first non-NULL value provided.
SELECT *
FROM tablename
WHERE (COALESCE(`tablefield`, 'value') = 'value')

CASE condition in SELECT statement in MySQL

I have CASE condition in SELECT. Below is the query condition
The query is from a view
SELECT expt_id,stain_type,control_stain, test_stain
CASE WHEN stain_type = 'Blue' THEN control_stain = 'NA'
CASE WHEN stain_type = 'Hemat' THEN test_stain = 'NA'
FROM experiment_results__view
Here the value 'NA' has to be populated for the condition of stain_type. Would appreciate your help with suggestions on how to write this query
Thanks!!
I expect you want to use something in the line:
SELECT
expt_id,
stain_type,
CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE NULL END control_stain,
CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE NULL END test_stain
FROM experiment_results__view
to get your value of 'NA' into columns named control_stain and test_stain.
But if you want to override the existing value in those columns with the value 'NA' in just those cases, then use:
SELECT
expt_id,
stain_type,
CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END control_stain,
CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END test_stain
FROM experiment_results__view
If you're intending an update of your table then you should rewrite this to:
UPDATE
experiment_results__view
SET
control_stain = CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END,
test_stain = CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END
-- if you don't want to update the complete table add an WHERE clause
WHERE
<some condition>
you need to END each case statement
SELECT expt_id,stain_type,
CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END AS 'Control Stain'
CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END AS 'Test Stain'
FROM experiment_results__view
if you are intending to update the table you can do an update query
UPDATE
experiment_results__view
SET
control_stain = CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END,
test_stain = CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END