MySQL don't read second OR condition if the first one is true - mysql

I'm trying to make an SQL-statement where I get the parameters from c# code.
The problem is that some of the parameters can be 'null'. But in MySQL I can't properly read WHERE column=NULL and I can only do WHERE column IS NULL.
So I was trying to make the statement like this:
SELECT *
FROM table
WHERE column=param #(param given from C#)
OR column IS param
I thought this would work fine because MySQL shouldn't care about the second condition if the first one is already true (like Python). And it does work when param is null. But there's an error when param is a string because then MySQL reads: WHERE column IS 'string'.
Is there a way to solve this?

You seem to want <=>, the null-safe equality operator:
WHERE column <=> param
Basically, NULL <=> NULL is true, while NULL = NULL is undefined.
Note that you should be properly passing your parameter to the query, using a prepared statement. String 'null' is not the same thing as a null value.

Related

Mysql select query if condition parameter is null then ignore the parameter

I have the following condition (m.idAgent = agent OR agent is null). It simply returns me all agents if i pass null to agent param.
But how can i achieve the same thing in MYSQL.
I.e if i pass null then that part of the where condition should be ignored and only the other conditions which are not null should be considered.
You could use COALESCE as follows:
WHERE COALESCE(agent, m.idAgent) = m.idAgent
This should work because when agent is NULL, it would just be replaced with the RHS of the comparison, and therefore would always pass. I presume that the following is actually what your code looks like:
WHERE COALESCE(?, m.idAgent) = m.idAgent
Here the ? is a placeholder for a value to be bound in the statement.

IFNULL() Equivalent in PostgreSQL

I am working on a project of migrating from MySQL to PostgreSQL, some function can't works well in PostgreSQL like IFNULL function. Some tutorials say that in PostgreSQL we can use NULLIF to handle it.
When I try I get an issue "argument of NOT must be type boolean, not type integer".
This is the simple SQL :
SELECT * FROM `tableA` WHERE not(nullif(columnA, 0));
How to solve it? Maybe some one can make an explain how can it works well. Thanks
NULLIF() is very different from IFNULL(). I think that what you want is COALESCE(), which will return the first non-NULL argument (it can have more than 2 arguments):
SELECT *
FROM table_a
WHERE NOT (COALESCE(column_a::boolean, false));
Reference: 9.17.2. COALESCE
Also, in Postgres you need to use true or false. 0 and 1 do not work for boolean literals. That's the reason that you get the error:
argument of NOT must be type boolean, not type integer
If column_a is an integer, then you have to CAST it to boolean. That's what column_a::boolean in the example above does. It is equivalent to CAST(column_a AS boolean).

how to check if a variable is defined in hive from within the view?

I'd like to setup a <check if='...'> where the if statement is defined on a param being defined. Is there a way to access this directly using the view?
You can use the variable on the if condition, it works:
<check if="#doesnotexist">
<true>The variable exists (?!)</true>
<false>The variable does not exist</false>
</check>
This is possible because when you try to retrieve a value from the F3 Hive, it returns NULL when the key does not exist. Per documentation, NULL is considered false for this verification:
An F3 expression inside an if attribute that equates to NULL, an empty string, a boolean FALSE, an empty array or zero, automatically invokes <false>

Is there a SQL mode for MySQL allowing "WHERE x = NULL" (meaning "WHERE x IS NULL")?

I know that using column = NULL in an SQL statement´s WHERE clause results in an empty result set.
As this is very inefficient in programming environments, I´m searching for a way to let MySQL interpret column = NULL like column IS NULL, maybe by setting an SQL mode?
You can use <=> instead. I wouldn't though. This is not portable.
(Thanks to Martin Smith who pointed me to this which was a solution to my problem)
You can have a case statement in your WHERE clause to use the correct syntax:
http://www.postgresql.org/docs/9.4/static/functions-conditional.html
SELECT * FROM my_items WHERE
CASE WHEN 1=1 THEN mac = '08:00:2b:01:02:03'
WHEN 1=2 THEN mac IS NULL
END;
Obviously, the first case (WHEN 1=1) will always be called in this example. You can put your own logic in that satisfies your condition.
Well, at least PostgreSQL does have a parameter for that
transform_null_equals = true
When is set to true, the query parser will transform
field = NULL
to
field is NULL
beforehand. Something tells me there's an equivalent parameter hidden in MySQL less documented params.
EDIT as of 2020-07-15
My "not really a solution" might be one of my first answers at SO. I'm somewhat ashamed for answering something about another DBRMS engine instead of what I was asked by the OP.
Anyway, the correct answer would be using the <=> operator as others have said in their answers already. You replace = for that and it will behave as other comparisons:
A = B // zero if different, one if equals, this is treated as a boolean result
A != NULL // always null, therefore not false nor true.
A <=> B // zero if different, one if equals, zero is one of the variables are null, 1 if both are.
This is not a setting and therefore it's behavior would never be session-wise or DDBB wise.

Simple query condition in sql

I have a Query - - < This Is a valid Query >
'SELECT *
FROM MyTable
WHERE city= ?
ORDER BY name ' [Keyname]
I am using this queried condition :: i am passing the Keyname as params from client to this sql query
This works & i get the required result BUT
If i pass nothing say null comes from client as param value for Keyname......... this query fails
how can i make the better query ... so that even if null comes ....
ORDER BY condition is satisfied
Or
R there other solution i need to look for
If so ... what is it ?
Hope i am clear
[EDIT]
CASE1:: for the url
http://54.218.73.244:7005/DescriptionSortedSearchRating/?Key=Pune
my told query satisfies::
But
http://54.218.73.244:7005/DescriptionSortedSearchRating/?Key=
my query fails, my sql query is expecting a Key for http://54.218.73.244:7005/DescriptionSortedSearchRating/ ..... if i pass nothing my query dosent get me a result..
.
what i am trying to see is even if i get nothing as key ORDER BY condition must be met ...
IF I PASS A KEY VALUE
IF I DONT PASS A KEY VALUE
You can clearly see i am not able to fetch results from database (Empty JSON)
This question doesn't have anything to do with MySQL. This is 100% your high level language. The null value the [Keyname] has is a null value in the language you're using to create the string that will be the final query.
The simplest solution will be not to assign null to your [Keyname] variable but rather an empty string.
You may use this:
ORDER BY name CASE WHEN Keyname IS NULL THEN '' ELSE CONCAT(',', Keyname) END
I am not sure whether the syntax is fine or not. But what I expect here to append empty string when Keyname is null and to append the Keyname with a comma (,). Please try it.
Other option is using function ISNULL
ISNULL(Keyname, '');