MySql, CONCAT and NOT IN, string values - mysql

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.

Related

How to replace a column with null values in mysql

One of my columns in the table contains 'NULL' values and I would like to replace them with certain values.
I looked at this post: Replace nulls values in sql using select statement? and followed the answer.
My sql statement:
Select ifnull(`option`, 'MCQ') as `option`
from question_table
This statement returns me the columns there are already with 'MCQ', but the 'NULL' values are not replaced yet.
Need some guidance to change this.
If you want to change the data, you need an update:
update question_table
set option = 'MCQ'
where option is null;
A select statement does not change the database.
If you want to update the table use Gordon's answer, but maybe you just want to return a replacement value for NULL in the SELECT, you can use COALESCE:
SELECT COALESCE(`option`, 'MCQ') as `option`
FROM question_table
This selects MCQ for every option-value that is NULL.
Another way is using CASE:
SELECT CASE WHEN `option` IS NULL THEN 'MCQ' ELSE `option` END as `option`
FROM question_table
'NULL' is a string, NULL is a special value that means "unknown/unavailable". They are totally different things.
MySQL function IFNULL() handles NULL values (they cannot be compared using the regular comparison operators). You can use the regular comparison operators (=, <>) to work with strings, even when they are 'NULL'.
If your query produces 'NULL' values in the result set it means the values in the database are not NULL but strings ('NULL').
In this case the query you need is:
SELECT IF(`option` = 'NULL', 'MCQ', `option`) AS `option`
FROM question_table

NULL to zero with ISNULL

I have the following SQL statement:
SELECT name, SUM(growth) AS sum_buy_price, SUM(recovery) AS sum_msrp, SUM(growth)+SUM(recovery) AS total
FROM orders
WHERE id = ?
GROUP BY name
My data is coming from a CSV file that I have no control over and either 'growth' or 'recovery' can be NULL in the data, but not at the same time. I need to use ISNULL to convert the possible NULL values to zero in order for the SUM to work correctly, but I'm unsure of how/where to add the ISNULL since the SELECT is indexing another record (name).
ISNULL returns whether the argument passed is null (i.e., it is analogous to true or false). I suppose, what you need is IFNULL:
SELECT
name,
SUM(IFNULL(growth, 0)) AS sum_buy_price,
SUM(IFNULL(recovery, 0)) AS sum_msrp,
SUM(IFNULL(growth, 0))+SUM(IFNULL(recovery,0)) AS total
FROM
orders
WHERE
id = ?
GROUP BY
name
The SUM() function ignores NULL values, so you don't need to change a NULL to a 0 in order for it to work properly.
If however, all values that you're aggregating are NULL and you want to return a 0 instead of NULL you can use IFNULL() or the more common COALESCE() to show 0 as the sum instead of NULL:
SELECT COALESCE(SUM(growth),0)
ISNULL() is a valid SQL Server function, IFNULL() is the equivalent in MySQL, but all major databases make use of COALESCE() which returns the first non-NULL value in a set, ie: COALESCE(NULL,NULL,5) would return 5.
This should work for you:
SELECT name, SUM(ISNULL(growth, 0)) AS sum_buy_price,
SUM(ISNULL(recovery, 0)) AS sum_msrp,
SUM(ISNULL(growth, 0))+SUM(ISNULL(recovery,0)) AS total
FROM orders
WHERE id = ?
GROUP BY name

MySql Query Replace NULL with Empty String in Select

How do you replace a NULL value in the select with an empty string?
It doesn't look very professional to output "NULL" values.
This is very unusual and based on my syntax I would expect it to work.
I'm hoping for an explanation why it doesn't.
select CASE prereq WHEN (prereq IS NULL) THEN " " ELSE prereq end from test;
Example of what the original table looks like, what I want, and what actually prints:
original wanted what actually prints
-------- ------ ---------------------
value1 value1
NULL NULL
value2 value2
NULL NULL
As you can see it does the opposite of what I want, hence I tried flipping the IS NULL to IS NOT NULL and of course that didn't fix it. I also tried swapping the position of when case, which did not work.
It seems the 3 solutions given below all do the task.
select if(prereq IS NULL ," ",prereq ) from test
select IFNULL(prereq,"") from test
select coalesce(prereq, '') from test
If you really must output every values including the NULL ones:
select IFNULL(prereq,"") from test
SELECT COALESCE(prereq, '') FROM test
Coalesce will return the first non-null argument passed to it from left to right. If all arguemnts are null, it'll return null, but we're forcing an empty string there, so no null values will be returned.
Also note that the COALESCE operator is supported in standard SQL. This is not the case of IFNULL. So it is a good practice to get use the former. Additionally, bear in mind that COALESCE supports more than 2 parameters and it will iterate over them until a non-null coincidence is found.
Try below ;
select if(prereq IS NULL ," ",prereq ) from test
Some of these built-in functions should work:
COALESCE(value,...)
Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
IS NULL
Tests whether a value is NULL.
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.
select IFNULL(`prereq`,'') as ColumnName FROM test
this query is selecting "prereq" values and if any one of the values are null it show an empty string as you like
So, it shows all values but the NULL ones are showns in blank
The original form is nearly perfect, you just have to omit prereq after CASE:
SELECT
CASE
WHEN prereq IS NULL THEN ' '
ELSE prereq
END AS prereq
FROM test;
Try COALESCE. It returns the first non-NULL value.
SELECT COALESCE(`prereq`, ' ') FROM `test`
Try this, this should also get rid of those empty lines also:
SELECT prereq FROM test WHERE prereq IS NOT NULL;
I have nulls (NULL as default value) in a non-required field in a database. They do not cause the value "null" to show up in a web page. However, the value "null" is put in place when creating data via a web page input form. This was due to the JavaScript taking null and transcribing it to the string "null" when submitting the data via AJAX and jQuery. Make sure that this is not the base issue as simply doing the above is only a band-aid to the actual issue. I also implemented the above solution IFNULL(...) as a double measure. Thanks.
UPDATE your_table set your_field="" where your_field is null

Is null condition checking in a select query

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;

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)