The fact is that SQL does not offer XOR by default. This is great what MySQL does because it is such a common feature. There is a simple way to โimplementโ XOR when you have two booleans:
... WHERE (foo = 1) = (bar = 0)
This gives you the following:
+-----+-----+-----+ | foo | bar | = | +-----+-----+-----+ | 0 | 0 | 0 | | 1 | 0 | 1 | | 0 | 1 | 1 | | 1 | 1 | 0 | +-----+-----+-----+
To invert the results of the = column, use the opposite <> operator as follows:
... WHERE (foo = 1) <> (bar = 0)
Now you have the XNOR :
+-----+-----+-----+ | foo | bar | <> | +-----+-----+-----+ | 0 | 0 | 1 | | 1 | 0 | 0 | | 0 | 1 | 0 | | 1 | 1 | 1 | +-----+-----+-----+
You can also configure the values โโas in:
... WHERE (foo = 1) = (bar = 1)
Therefore, if you use a cleaner SQL database such as PostgreSQL that does not offer logical XOR (as of 2019), this simple solution for you is much simpler than the full one:
(A AND (NOT B)) OR ((NOT A) AND B)
where you have to repeat A and B and this can be very expensive in terms of filtering your results, so it was probably decided not to include the XOR operator in SQL.
Alexis wilke
source share