Is null condition checking in a select query - mysql

Tablename : Employee
select name,age,photopath from employee;
I need to re frame the query, ie if the photo path is null, then i need to return photo cloumn which is a BLOB.
photopath - will be http://www.servername.com/imagename.html
Thanks.

You can use IF() to check for the condition, and return one of two columns. Possibly in combination with AS to create an unique field name (if that is your goal).

SELECT IF(field1 IS NULL, field2, field1) FROM table1;

Related

Where to put aliases in MySQL query

I'm quite new to MySQL and SQL in general and can't fully understand how and where to place aliases.
For examlple I have scheema like this:
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE table_a (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
value INT NOT NULL,
PRIMARY KEY(id)
);
CREATE TABLE table_b (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
value INT NOT NULL,
PRIMARY KEY(id)
);
INSERT INTO table_a (value) VALUES (25), (43), (15);
INSERT INTO table_b (value) VALUES (11), (15), (16);
And I need to get sum of values of every record(in both tables). For given example it should be 125. My query looks like this
SELECT SUM(value) FROM
(
SELECT * FROM
(SELECT value FROM table_a) AS a_value
UNION ALL
(SELECT value FROM table_b) #AS b_value
) as total;
What I can not understand is:
why "a_value" and "total" aliases are absolutely necessary (I never use them in this query) and their absence giving me "Every derived table must have it's own alias"?
why "b_value" is making "SELECT is not valid at this position" error for first select? Isn't it also a derived table like one with "a_value" alias?
And I have one question about parentheses and subqueries: why do I need this "SELECT * FROM" to take UNION in parentheses? I started from such request:
SELECT SUM(value) FROM
(SELECT value FROM table_a) AS a_value
UNION ALL
(SELECT value FROM table_b);
But instead of sum I want it was giving me sum of "value" from "table_a" united with "value" column from "table_b" - [83, 11, 15, 16]. So I tried just take everything after FROM in parentheses and it didn't work out: I got "SELECT is not valid at this position" error.
I wasn't really sure, just tried first that came to my mind and put this SELECT * FROM inside parentheses before everything else and it worked. So I want to know why it works and why my first way (with just parentheses) does not work?
All derived tables -- subqueries in the FROM clause -- need to have a table alias. It doesn't make a difference if they are ever used.
However, the subqueries are not needed. You can put the UNION ALL in the FROM clause:
SELECT SUM(value) as total
FROM (SELECT value FROM table_a UNION ALL
SELECT value FROM table_b
) ab
-------^ note that this is needed although it is not used in the query
The alias could be used:
SELECT SUM(ab.value) as total
It might be slightly faster to do the aggregation first in the subquery:
SELECT SUM(value)
FROM (SELECT SUM(value) as value FROM table_a UNION ALL
SELECT SUM(value) as value FROM table_b
) ab
It is because the result from the sub-query can it can be uniquely identified. You have to do this even if you are not going to reference the name anywhere.
When subqueries are used in a SELECT statement they can only return one value.
In general, the subquery is run only once for the entire query, and its result reused.

MySql, CONCAT and NOT IN, string values

I have such query but it doesn't select anything, but it should. So query
SELECT *
FROM _custom_access_call
WHERE CONCAT(type, name) NOT IN ('string1', 'string2', 'string3')
I manually add to table entry with null and '1sfgsg' values but it wasn't selected. Why? I need to select all entries that concat values is not in array. Help to deal with it.
If one of the values is NULL, then CONCAT() will return NULL. And NULL NOT IN (...) is always NULL. Also NULL IN (...) is always NULL. If you want to use NULL you should explicitally handle it. In this specific case, CONCAT_WS() helps, because it never returns NULL.
SELECT *
FROM _custom_access_call
WHERE CONCAT_WS('', type, name) NOT IN ('string1', 'string2', 'string3');
Also, note that this query cannot use any index.

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

mysql NULL value in where in CLAUSE

how to deal with NULL value in mysql where in CLAUSE
i try like
SELECT * FROM mytable WHERE field IN(1,2,3,NULL)
it not working
only work like :
SELECT * FROM mytable WHERE field IN(1,2,3) OR field IS NULL
how can i get it work in WHERE IN ? it is possible ?
There is a MySQL function called COALESCE. It returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
If you for example run SELECT COALESCE(NULL, NULL, -1); you will get -1 back because it's the first non-NULL value.
So the trick here is to wrap your expression in COALESCE, and add a value as the last parameter that you also add in your IN function.
SELECT * FROM mytable WHERE COALESCE(field,-1) IN (1,2,3,-1)
It will only match if field is 1,2 or 3, or if field is NULL.
As by my understanding you want to pull every record with 1,2,3 and null value.
I don't think its possible to put null in the IN operator. Its expects values and null is well.. not a value. So You really have to put the OR with the null to get the desired result.
Maybe this information from the MySQL Reference Manual helps:
To comply with the SQL standard, IN returns NULL not only if the expression on the left hand side is NULL, but also if no match is found in the list and one of the expressions in the list is NULL.
Using UNION as a subquery in IN operator can get tableIds as a list and from that can get results with the NULL value.
eg:
SELECT * FROM
mytable
WHERE mytable.id IN(
SELECT mytable.id
FROM mytable
where mytable.field IS NULL
UNION
SELECT mytable.id
FROM mytable
WHERE mytable.field IN(1,2,3)
)
Following statement should help:
SELECT * FROM mytable WHERE COALESCE(field,0) IN (1,2,3,0)

MySQL COUNT() and nulls

Am I correct in saying:
COUNT(expr)
WHERE expr IS NOT *
Will count only non nulls?
Will COUNT(*) always count all rows? And What if all columns are null?
Correct. COUNT(*) is all rows in the table, COUNT(Expression) is where the expression is non-null only.
If all columns are NULL (which indicates you don't have a primary key, so this shouldn't happen in a normalized database) COUNT(*) still returns all of the rows inserted. Just don't do that.
You can think of the * symbol as meaning "in the table" and not "in any column".
This is covered in the MySQL Reference Manual.
If you want to count NULLs as well, try
SELECT COUNT(IFNULL(col, 1)) FROM table;
just checked:
select count(*)
returns 1 with one record filled with NULLs
select count(field)
returns 0.
I don't see the point in the record with NULL values. Such record must not exist.
count(*) is not for non-null columns, it's just the way to ask to count all rows. Roughly equivalent to count(1).
Using MySQL I found this simple way:
SELECT count(ifnull(col,1)) FROM table WHERE col IS NULL;
This way will not work:
SELECT count(col) FROM table WHERE col IS NULL;
If you want to count only the nulls you can also use COUNT() with IF.
Example:
select count(*) as allRows, count(if(nullableField is null, 1, NULL)) as missing from myTable;
You can change the if condiditon to count what you actually want. So you can have multiple counts in one query.
select count(*) as 'total', sum(if(columna is null, 1, 0)) as 'nulos' from tabla;