Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using exclusive or in mysql

Tags:

mysql

I have a table as below

Table foobar

|foo|bar|
---------
| 1 | 1 |
| 1 | 0 |
| 0 | 1 |
| 0 | 0 |

I need to be able to do something similar to this

select * from foobar
where foo = 1 or bar = 1

Which would return the below

|foo|bar|
---------
| 1 | 0 |
| 0 | 1 |

Meaning that what is returned is exclusively 1 for the value. Is there such a thing as XOR in mysql that functions like this?

like image 384
TheMightyLlama Avatar asked Jan 17 '14 08:01

TheMightyLlama


2 Answers

Yes, XOR. MySQL XOR operator checks two operands (or expressions) and returns TRUE if one or the other but not both is TRUE.

select * from foobar
where foo = 1 XOR bar = 1

The actual mathematical representation of a XOR equation "A XOR B" is "(A AND (NOT B)) OR ((NOT A) AND B)".

like image 153
Linga Avatar answered Oct 27 '22 02:10

Linga


try like this:

select * from test where foo = 1 XOR bar = 1;

Sql Fiddle: http://sqlfiddle.com/#!2/079cc/4

like image 44
Suresh Kamrushi Avatar answered Oct 27 '22 03:10

Suresh Kamrushi