MySQL solution for using CASE WHEN with another CASE WHEN - mysql

I'm having troubles with the following query structure (I can't run it because I don't know how to do it).Simple form of it goes like this:
SELECT a,b,
CASE WHEN a=x AND b=y THEN "Something"
ELSE "Something Else"
END AS "1st Case",
CASE WHEN "1st Case"= "Something" THEN "Something New"
ELSE "Other"
END AS "2nd Case"
FROM table1
I thought of CTE but I can't use it with our server version. I thought of sub-query but don't know how to reference my custom column (result of "1st Case") in outer query.
Please help me with this, I guess, simple issue.

Try this:
select x.*, CASE WHEN 1stCase= 'Something' THEN 'Something New'
ELSE 'Other'
END AS '2nd Case' from
(SELECT a,b,
CASE WHEN a=x AND b=y THEN 'Something'
ELSE 'Something Else'
END AS 1stCase from table1)x

Related

How to use placeholder in sql to another column in select

i want to make a report and i already calculate it in one column then i want to use the result to another column, something like this
select addtime (timediff(a,b), c) as 'total_lead', case when
total_lead <= then 'yes' else 'no' end as 'check data' from d
so i want to use the result and use it in another column
anyone can help?
You have to either repeat the expression:
select
addtime(timediff(a,b), c) as 'total_lead',
case when addtime(timediff(a,b), c) <= ? then 'yes' else 'no' end as 'check data'
or use a subquery, which may change the performance:
select
total_lead,
case when total_lead <= ? then 'yes' else 'no' end as 'check data'
from (
select
addtime(timediff(a,b), c) as 'total_lead'
from d
) d_with_total_lead

Using something similar to a CASE WHEN in Phalcon query builder

$fileQueryBuilder->columns(
[
"id" => "d.discovered_file_id",
"company_name" => "d.company_name"
]
);
This is the part of my query builder where I mention the column names to be selected/displayed. Can I handle the 'company_name' field to show it's value if it has one,
and something like 'Not available' if it's empty, in this part of the query builder itself? Is there a way of doing that, like using a CASE WHEN same as in SQL?
What I tried- CASE WHEN d.company_name IS NOT NULL THEN d.company_name ELSE 'Not available' END => d.company_name
, but this doesn't work.
Isn't IF better in this case?
IF(d.company_name IS NOT NULL, d.company_name, 'Not available') as company_name
Also PHQL only supports case syntac like this:
CASE column WHEN value THEN some expression ELSE some expression END

I need an SQL query that selects from either Column A or B depending on the existence of a string in Column C

I need an SQL query that selects from Column A if Column C contains the string 'ebook'. Otherwise, select from Column B.
So something like:
IF (Table.ColumnC = "ebook") SELECT Table.ColumnA AS Publisher
ELSE SELECT Table.ColumnB AS Publisher
Select
case when columnC = 'e-book'
then columnA
else columnB
end as Publisher
from myTable;
You need a case statement
I reformatted to help show how this works.
Basically Case is a scalar (row per row) IF statement
There can be multiple conditions WHEN, similar to else if. The function goes from condition to condition, if no conditions are passed, the ELSE value is used.
There are simple and search forms of CASE Documentation
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
select case columnC when 'ebook' then columnA else columnB end as publisher
from my_table;
In MySQL you can write it like this:
SELECT IF(Table.ColumnC = 'ebook', Table.ColumnA, Table.ColumnB) AS Publisher FROM Table...
If you have more than one possibility, then go for CASE..WHEN..THEN..ELSE..END.

MySQL - CASE vs IF Statement vs IF function

Who can please explain the difference between CASE-statement, IF-statement and IF-function?
What is the difference in terms of usage and "how it works"?
From the manual, it looks like the if function is just a less flexible form of the case expression. For example, you could write:
select if(username = 'darxysaq', 'high', 'low') as awesomeness
And the equivalent with case:
select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness
But case is more flexible. It allows more than one branch, like:
select case
when username = 'darxysaq' then 'high'
when username = 'john skeet' then 'medium'
else 'low'
end as awesomeness
And it can act like a switch:
select case username
when 'darxysaq' then 'high'
when 'john skeet' then 'medium'
else 'low'
end as awesomeness
Now the if statement is an entirely different beast. It is a control statement in MySQL procedures. The statement form looks like:
CREATE FUNCTION GetAwesomeness (username varchar(50))
RETURNS varchar(20)
BEGIN
IF username = 'darxysaq' THEN
return 'high';
ELSEIF username = 'john skeet' THEN
return 'medium';
ELSE
return 'low';
END IF;
END; //
Here's a SQL Fiddle with the statement version. It looks like Mr Bean isn't all that he's made up to be!
A final note: the case expression is standard SQL and works in most databases. The if function is not standard SQL and will not work in other databases, like SQL Server or PostgreSQL.

Using comparison as alias in select for Doctrine2

Trying to do this in Doctrine2:
...->createQuery('SELECT m.id, (m.status != 1) as verified...
But that throws an error - if I take parenthesis off I get another error. How do I achieve this m.status comparison?
Thanks
Doctrine 2 doesn't support these comparisons in the SELECT clause (at least not up to 2.3, not sure about 2.4).
You can use a CASE expression as workaround:
SELECT m.id, CASE WHEN m.status != 1 THEN 1 ELSE 0 END AS verified ...
or:
SELECT m.id, CASE WHEN m.status = 1 THEN 0 ELSE 1 END AS verified ...
If you need verified for an ORDER BY clause (or something like that), but don't actually need it in the result, you can use the HIDDEN expression:
SELECT m.id, CASE WHEN m.status = 1 THEN 0 ELSE 1 END AS HIDDEN verified ...
A completely different solution is to write a custom DQL function.
You can use the solution proposed here: Cumulative DQL with Doctrine
When working with entities, keep in mind that adding selects will make the query return an array for each result:
$res = $em->createQueryBuilder()
->from('BlogPost', 'p')
->select('p')
->addSelect('(2+3) AS dummy')
->getQuery()->getResult();
Iterating over $res will return an array:
foreach($res as $mixed){
echo get_class($mixed[0]); //$mixed[0] contains the BlogPost
echo $mixed['dummy']; //displays the dummy result (5)
}
check this out: 13.2.4. Using Expr* classes to create conditionals
using Expression methods you could do something like:
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select('m.id')
->from('Entities\MyEntity', 'm')
/*neq() is the "not equal" comparison function*/
->where($qb->expr()->neq('m.status', $someValue)),
->getQuery();