MySQL return case value - mysql

I'm looking for a way to return the value that was "case"-ed on.
e.g. what will return me the md5 result without recomputing it at the then statement
SELECT
CASE md5(col1,col2,col3,...,coln)
WHEN MD5('') then NULL
else ???
end

If you do not want to repeat a calculation multiple times in the select list, then you need to push it into a subquery and reference the field alias set in the subquery:
SELECT
CASE t1.calc
WHEN MD5('') then NULL
else t1.calc
end as md_5
FROM
(select md5(col1,col2,col3,...,coln) as calc from table) t1

Related

Replace null with zero in sql query

I have an sql query that could potentially return null values, in the event of this I want the query to return '0'. Here is the query
SELECT (select count(goal) from fixtures where goal='1' and fixture='$fixture') as goalCountHome
from fixtures where fixture='$fixture'LIMIT 1
Any help much appreciated!
In MySql use IFNULL() function. For MsSql use ISNULL() function.
If you are using MySql, IFNULL(<column_name>, 0) should do.
This query:
SELECT (select count(goal) from fixtures where goal='1' and fixture='$fixture') as goalCountHome
FROM fixtures
WHERE fixture = '$fixture'
LIMIT 1
cannot return NULL values. The subquery is an aggregation query with no GROUP BY, so it always returns one row. That row will contain a result from COUNT(). COUNT() itself can never return a NULL value. If there are no rows, then the value will be zero.
The outer query might return no rows but that is different from NULL values.
Of course, this query is way overcomplicated, and should simply be:
SELECT COUNT(*) as goalCountHome
FROM fixtures
WHERE fixture = ? AND -- pass this in as a parameter
goal = 1 ; -- it looks like a number so I assume it is
Note that you should be passing parameters in using proper parameters rather than munging query strings.
if you need all the rows and not the rows where goal is not null you could use count(*)
select count(*)
from fixtures
where goal=1
and fixture='$fixture'
count(goal) return the number of rows where goal is not null
count(*) return the total number rows selected
otherwise in general when you need not null values in mysql you can ifnull(your_column, value) or coalesce(your_column, value)
based on you comment seems you need sum(goal)
select sum(ifnull(goal,0))
from fixtures
where goal=1
and fixture='$fixture'

How to select zero result mysql?

I want to return a select query with zero result. When I type this query:
Select NULL .... from ....
It returns NULL value as a result. But I want to return zero result instead of NULL. So, how to do that? I use coalesce but if I have value, it doesn't return zero. So, what should select query use to return zero value? I use MYSQL.
If you want to add column to existing columns you can use the below instead.
Select *,0 as ZeroColumn from ....
I can't imagine why you want to do this, but if you do it's quite simple. SELECT 0 will return zero once. SELECT 0 FROM MyTable will return zero once for each record in MyTable.

Mysql SUM inside a CASE

Say I have a query that is something like this:
select
table1.column1,
table1.column2,
...,
case when foo.id is null then 'N/A' else sum(foo.points) end as score -- same result using ifnull()
from
table1 inner join table2 on ...
left join foo on ...
group by ...
Since I do a LEFT JOIN on foo, there is a chance that there is no match. In that case, I want the score to show as 'N/A' instead of NULL or 0. But when I do the above query, I get blob for the score column for all rows.
When you have different data types in the results in the case it will return a value with the data type blob if it can't find a common type.
Try casting the sum to a char:
case when foo.id is null then 'N/A' else cast(sum(foo.points) as char) end as score
If you are grouping, you should really put your sum around the case, like:
sum(case when foo.id is null then 0 else foo.points)
..as otherwise you are summing just the row value (meaning only one value).
Also, a column should only have one data type, so either number or text, which is why you might be seeing this issue since you are trying to display either a number or text in the same column.
If you really want N/A, you can try converting the number to text, and then using the coalesce function to handle nulls, however I would need to see your data to say the best way to write your above query. If you can create an SQL fiddle, I would be more than happy to take a look :)
SUM up the the results of the CASE..WHEN.
SUM(CASE WHEN foo.id IS NULL THEN NULL ELSE foo.points) END AS score
You can display the default value in the frontend application (n/a) when the score field is null (or equals to 0).
The score will be NULL when there all rows has null in foo.id.

SQL query and error Subquery returned more than 1 value

Hello here is my SQL query :
WHERE table1.Regnumber IN (
CASE
WHEN #fRegNumber IS NOT NULL AND #fRegNumber<>-1 THEN #fRegNumber
ELSE (SELECT Regnumber FROM table2)
END
)
An error:
'Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.'
How can I fix this?
The problem is with this subquery.
(SELECT Regnumber FROM table2)
It's returning every regnumber from the table. In the context of the overall query, you have to pick just one. The first step in solving your problem is to decide which one you want and why.
Change to:
WHERE IF(#fRegNumber IS NOT NULL AND #fRegNumber<>-1,
table1.Regnumber = #fRegnumber),
table1.Regnumber IN (SELECT Regnumber FROM table2))
WHERE x IN can be used in two ways:
WHERE x IN (expr1, expr2, expr3, ...)
In this case, if you use a subquery as the expression, it must return just a single value. A subquery that returns multiple values will not be spliced in.
or:
WHERE x IN (subquery)
This is the format that allows a subquery to return multiple rows, and x will be tested against all of them.
Since you want to conditionalize whether to test against a single value or the results of a subquery, you must do that outside the IN clause, using IF.
There may be other ways to write this query as a LEFT JOIN.
I can see 2 approaches here. One keeping the CASE statement this way:
WHERE
CASE WHEN COALESCE(#fRegNumber, -1) == -1
THEN t1.regnumber IN (SELECT t2.regnumber FROM table2))
ELSE t1.regnumber = #fRegNumber
END
And the other one rephrasing the whole CASE statement into the appropriate logical operators:
WHERE
(COALESCE(#fRegNumber, -1) != -1 AND t1.regnumber = #fRegNumber) OR
(COALESCE(#fRegNumber, -1) == -1 AND t1.regnumber IN (SELECT t2.regnumber FROM table2))

How to return NULL when result is empty?

I have a simple query that selects one field and only one row, thus one value.
Is there any way to make it return NULL if the query results in an empty set? Instead of returning zero rows?
I think I need to use something with NOT EXISTS, THEN NULL but not certain about it.
select
(Your entire current Select statement goes here) as Alias
from
dual
dual is a built in table with a single row that can be used for purposes like this. In Oracle this is mandatory. MySQL supports it, but you can also just select a single value without specifying a table, like so:
select
(Your entire current Select statement goes here) as Alias
In either case you're selecting a single value. This means that:
If your select returns one value, that value is returned.
If your select statement returns one column, but no rows, NULL will be returned.
If your select statement returns multiple columns and/or multiple rows, this won't work and the query fails.
An easy way to do this is with aggregation:
select max(col)
from t
where <your condition here>
This always returns one row. If there is no match, it returns NULL.
Late reply but I think this is the easiest method:
SELECT
IFNULL((SELECT your query), NULL)
Use a UNION with a NOT EXISTS(original where clause)
select col1
from mytable
where <some condition>
union
select null
where not exists (
select * from mytable
where <some condition>)
You can use COALESCE for example:
SELECT COALESCE(Field1,NULL) AS Field1 FROM Table1
Edit 1:
sorry i mistake with return field as null not result set,for result set return as null use Union and Exist Function like this:
SELECT NULL AS Field1 FROM Table1 WHERE not EXISTS(SELECT Field1 FROM Table1 WHERE Field2>0)
UNION
SELECT Field1 FROM Table1 WHERE Field2>0