How to writer the condition A or B is zero , but not both is zero in MYSQL? - mysql

What is the correct syntax for the following SQL statement ?
SELECT * FROM TABLE WHERE (A = 0 AND B != 0) OR (A!=0 AND B=0)
Thanks
Updated: How about negative case? I would like to check if A or B is non-zero , but not both is non-zero, Thanks
SELECT OperateProductId FROM DPS_UserLoginStatus Where OperateProductId <> 0 XOR OperateIssueId <> 0

You're looking for the XOR logical operator:
SELECT * FROM TABLE WHERE A = 0 XOR B = 0
In terms of other operators, a XOR b (commonly a ^ b when used bitwise rather than logical), is equivalent to (a and !b) or (b and !a). It's also equivalent to: (a or b) and (!a or !b). In English, it's exactly what you're looking for: a xor b is true if and only if either a or b is true but both are not true.

Related

Simplify "all or nothing" boolean expression

Can a "all or nothing" boolean expression be simplified? Suppose I have three values, A, B, C, and want to determine if all three are true, or all three are false. Like a XOR gate, but with N values.
Can this statement be simplified?
(A && B && C) || !(A || B || C)
All true or all false basically means that all should be the same. So if the equality comparison is acceptable you can do this:
A == B && B == C

mysql list of columns check only one of those have value

I have a list of columns like a, b, c, d, e, f.
Ideally only one of those can have value.
How can I find out the rows that break this rule.
thanks.
Assuming that no value in a column means null (not empty string ''). We can utilize comparison operators IS NOT NULL. It returns 1 if the value is not null; else 1.
Sum of the above-mentioned comparison results for each of the columns should be equal to 1 only (if there is only one non-null column in a row).
SELECT *
FROM your_table_name
WHERE ((a IS NOT NULL) +
(b IS NOT NULL) +
(c IS NOT NULL) +
(d IS NOT NULL) +
(e IS NOT NULL) +
(f IS NOT NULL)) <> 1
DB Fiddle DEMO
You could use:
SELECT *
FROM tab
WHERE LENGTH(CONCAT(
IF(a IS NULL,'','.'),
IF(b IS NULL,'','.'),
IF(c IS NULL,'','.'),
IF(d IS NULL,'','.'),
IF(e IS NULL,'','.'),
IF(f IS NULL,'','.'))) <> 1;
db<>fiddle demo
Sounds like you should not have 6 columns for one value, but instead, 1 column, perhaps plus a second column to say which thing it represents (a..f).

How to display zero values in mysql query?

This code make an operation on a and b and check if result is equal to c.
If yes, just print this row. My question is how to change this code to display
rows when result (a / b) = 0.
SELECT id, a, b, operation, c
FROM expressions
where
case
when operation LIKE '+' AND (a + b = c) then c
when operation LIKE '-' AND (a - b = c) then c
when operation like '/' AND (a / b = c) then c
when operation like '*' AND (a * b = c) then c
ELSE FALSE END;
Output:
id a b operation c
1 2 3 + 5
4 4 7 * 28
11 0 1 / 0
14 239 0 * 0
15 18 18 - 0
1, 2 rows are ok and printed. 3, 4, 5 rows should be printed but they are not!
When a / b = 0 then second condition in sql query is false - row is not printed, e.g. 0 / 1 = 0. It is 0 and should be printed. In contrart to 1 / 0 which shouldn`t be printed.
My solution is to cast (a / b = c) to unsigned but it is not working?
You shouldn't mix types like boolean and int because implicit conversion occurs.
Use explicit value instead of TRUE/FALSE (0 is treated as FALSE).
SELECT id, a, b, operation, c
FROM expressions
where
case
when operation LIKE '+' AND (a + b = c) then 1
when operation LIKE '-' AND (a - b = c) then 1
when operation like '/' AND (a / b = c) then 1
when operation like '*' AND (a * b = c) then 1
ELSE 0 END = 1;
alternatively:
SELECT id, a, b, operation, c
FROM expressions
where
case
when operation LIKE '+' AND (a + b = c) then TRUE
when operation LIKE '-' AND (a - b = c) then TRUE
when operation like '/' AND (a / b = c) then TRUE
when operation like '*' AND (a * b = c) then TRUE
ELSE FALSE END;
As already pointed out, the issue is that 0 is treated as false.
I would simplify the logic to:
SELECT id, a, b, operation, c
FROM expressions
WHERE (operation LIKE '+' AND (a + b = c) ) OR
(operation LIKE '-' AND (a - b = c) ) OR
(operation LIKE '/' AND (a / b = c) ) OR
(operation LIKE '*' AND (a * b = c) );
I don't think the CASE makes the code more understandable.
If you are concerned about divide-by-zero, then use nullif():
(operation LIKE '/' AND (a / nullif(b, 0) = c) ) OR

Mysql Precedence Logic

Any explanation to the following queries :
Select x FROM y WHERE a = 1 OR a = 2 AND (b = 1 OR b = 2)
why it doesn't return the correct info while this return the correct info :
Select x FROM y WHERE (a = 1 OR a = 2) AND (b = 1 OR b = 2)
Am i missing something here ?
X Y (X OR Y) X OR Y
1 0 1 1
0 1 1 1
1 1 1 1
0 0 0 0
I know in term of precedence the () have priority , but why should i add them the the first part of the query ?
Correct me if I'm wrong
Thank you
AND has a higher precedence than OR so your first query is equivalent to this:
Select x FROM y WHERE a = 1 OR a = 3 OR (a = 2 AND (b = 1 OR b = 2))
Which is not equivalent to
Select x FROM y WHERE (a = 1 OR a = 2 OR a = 3) AND (b = 1 OR b = 2)
I guess you forgot the a = 3 part in your first query.
Operator precedence in MySQL
Because ambiguity is an undesirable trait?
Also, the optimizer will re-order your WHERE Conditions if it thinks it will perform better. Your ambiguity will, therefore, cause different results depending on how/what it evaluates first.
Always be explicit with your intentions.
Using parentheses in your WHERE clause does not just affect precedence but also groups predicates together. In your example the difference in results is more a matter of grouping rather than precedence.
You could think of this: (pN = predicate expression)
WHERE a = 1 OR a = 2 AND (b = 1 OR b = 2)
as:
WHERE p1 OR p2 AND p3
And this:
WHERE (a = 1 OR a = 2 OR a = 3) AND (b = 1 OR b = 2)
as:
WHERE p1 AND p2
and so it becomes clear that the results could be quite different.

If I XOR 2 numbers, do I only get identical results if the numbers are the same?

For example, suppose I have x XOR y = y XOR x = z. Is it possible to have something like a XOR b = z?
Short answer: Yes
Long answer:
XOR is a binary operation, it works on the individual bits and it's commutative.
It has the truth table:
A B Q
0 0 0
0 1 1
1 0 1
1 1 0
As the number is made up of these bits then the result will be the same as long as for each bit position the two bits have the same result.
For example take the 2 eight bit numbers 113 and 42
113 = 01110001
42 = 00101010
XOR = 01011011 = 91
but if I swap the fourth bit from the left I get
97 = 01100001
58 = 00111010
XOR = 01011011 = 91
So yes again...
Yes.
z = y because x ^ y ^ x = y
So it is entirely possible for a combination a ^ b = y = z.
In fact, for every a there exists a b such that a ^ b = z. To calculate that, b = z ^ a.
Be aware that XOR is commutative: this means that x ^ y = y ^ x.
Yes. As a degenerate proof, XORing a number with itself always results in 0.
XOR, will return true if both parameters are different, assuming that the parameters are Boolean values anyway. This is different from or, which will return true if either parameter is true, and NOR, which will return true only if both of them are false.