Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select when one condition or another are met but not both

Tags:

sql

select

Given the following table:

id      column1   column2
-------------------------
1       3         8
2       4         7
3       4         10
4       4         14
5       14        17
6       10        27
7       14        21
8       16        14
9       21        4
10      30        3

what is the nicest way to query selecting rows when number 4 and 14 are selected in column1 or column2 BUT exclude when number 4 and number 14 are both in the row. Be aware that the order can be reversed.

expected output

id      column1   column2
-------------------------
2       4         7
3       4         10
5       14        17
7       14        21
8       16        14
9       21        4
like image 433
Alfonso Fernandez-Ocampo Avatar asked Nov 25 '14 08:11

Alfonso Fernandez-Ocampo


1 Answers

SELECT * FROM table WHERE (column1=4 XOR column2=14) XOR (column1=14 XOR column2=4)  
like image 58
Javlon Tulkinov Avatar answered Sep 19 '22 15:09

Javlon Tulkinov