What is meaning of <> in MS-SQL? - mysql

I don't know what the is meaning of <> is in MS-SQL
For example:
IF(#userID <> '')
Return 0;
ELSE
Return -1;

In Transact SQL, <> means NOT EQUAL TO
Check msdn here

As it would logically imply:
x <> y
if x is greater then or less then y.
Even simpler, not equal.

As you ask for MYSQL, Its one of the comparison operator.
MYSQL Documentation for Comparison Operator
<> Means "Not equal operator"

<> means not equal to in Transact-SQL.
Compares two expressions (a comparison operator). When you compare nonnull expressions, the result is TRUE if the left operand is not equal to the right operand; otherwise, the result is FALSE.

<> in MS SQL means "NOT EQUAL TO".
In your case IF(#userID <> '') means IF variable #userID IS NOT EQUAL to '' THEN

As you can see here it is not equal

<> is a comparison operator meaning "not equal to"
This is used to compare two expressions and will result to a boolean value.
Thank you! :)

Related

MySQL - if condition optimization

Just wondering, how this query will be handled by MySQL, will sum() calculated twice if sum(credits) != NULL or does MySQL has optimization in place for such queries.
select if(sum(credits)=NULL, 0, sum(credits)) from ......
Thanks
If won't work. The first condition is always false. The correct way to compare to NULL is IS NULL, not =.
Just use this construct:
select coalesce(sum(credits), 0)
Using IF
SELECT IF(sum(credits) IS NULL, 0 ,sum(credits)) .....
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used
Using IFNULL
SELECT IFNULL(sum(credits), 0) .....
Using COALESCE
SELECT COALESCE(sum(credits), 0) .....
Refer : MySQL Control Flow Functions
This is something we shouldn't worry about. With SQL we tell the DBMS what to do, not how to do it.
It is very, very likely that the DBMS will compute sum(credits) only once. It would seem rather stupid if not. However, this is not dictated by the SQL standard, so the DBMS programmers are free to choose how to code this. They could write the DBMS such that sum(credits) is computed twice. We cannot know whether they did or not, but as mentioned it is not likely and we shouldn't worry about it either.

Mysql column comparison 'different from 0' but not from NULL

If I select a column from a MYSQL column, I try to check if it's different from zero (0) by the following constraint:
WHERE my_column_name <> 0
It returns false if it's zero (0) but also if it's NULL. And that's not the purpose.
How do I have to solve this problem?
NULL - safe comparison is done with <=>, you only need to add a NOT() as an alternative:
WHERE NOT(my_column_name <=> 0)

MySQL - NULL safe NOT equal operator

I am just curious - I know about NULL safe equal operator <=>, but is there some NULL safe NOT equal operator? Or I have to always use something like this:
(tab.id != 1 OR tab.id IS NULL)
or someone prefers
!(tab.id <=> 1)
I found that NOT (NULL <=> 1) works and I think it is also ISO standard compliant, but is cumbersome. A better way to show using column names would be like this: NOT (tbl.col1 <=> 1)
COALESCE(tab.id, 0) != 1
Can be used here if you like it. I goes through the parameters, and returns the first value that isn't NULL. In this case if it's NULL, it will compare 0 != 1. Although it may use more signs, it's still easier to manage instead of being forced to always have opposite "booleans" as a solution in those cases.
Read documentation for COALESCE()
Now MySQL does not have a NULL-safe not equal operator.
Using MySQL the most universal solution is:
!(tab.id <=> 1)
or
NOT tab.id <=> 1
because it will work properly if even in place of 1 you will use NULL.
If you know that the RHS of the comparison IS NOT NULL:
COALESCE(tab.id != 1, 1)
Or
COALESCE(tab.id != 1, TRUE)
will give you the correct result.
Whether this is better more readable than:
(tab.id != 1 OR tab.id IS NULL)
Is debatable..
I'd also be tempted to filter out the NULL logic first with comparisons, so I don't have to think about them! I'd probably write your first equation as:
(tab.id IS NULL OR tab.id != 1)
Performance will depend on prevalence of NULLs however.

Using <> in MySQL

I have come across <> used in a statement conditions a few times. Example:
SELECT param, d.param
FROM panel p join object d on p.id=d.id
WHERE param IS NOT NULL and param<>''
Let me confirm the statement above has not been tested, it mimics some of the statements that I have come across.
My question is, what is the meaning of the the diamond <> condition?
<> is like NOT .. = .. and !=. It means not equal.
MySQL comparison operators - not equal
its the opposite of "equal" so it is "not equal".
in C it is !=
so in your question it means param is not a empty string

Is there a performance difference between `=` and `<=>`?

I've recently changed all my where conditions to use <=> instead of = because I need to check against nulls. Are there any performance concerns?
There is no real performance impact here is a test to verify for yourself
mysql> SELECT BENCHMARK(1000000, (SELECT SQL_NO_CACHE userId FROM Activity WHERE userId<=>42459204 LIMIT 1));
Make sure that you need to use <=>
NULL-safe equal. This operator
performs an equality comparison like
the = operator, but returns 1 rather
than NULL if both operands are NULL,
and 0 rather than NULL if one operand
is NULL.
If you just need to check the rvalue do
col=CONST AND CONST IS NOT NULL
or t1.col=t2.col
<=> is basically a shortcut to include OR (Val1 IS NULL AND Val2 IS NULL) or IS NOT DISTINCT FROM
It is an additional operation but the difference should be negligible unless you are SELECTing the data to be compared because otherwise the first SELECT returning NULL doesn't need to execute the second SELECT because the standard equality operator = will always yield false.
As #Dathan noted, make sure this is actually when you intend to do.