MYSQL syntax for OR?

I have a mysql table with 2 columns and a set of values ​​called MAIN. table1:

col1, int(11)
col2, int(11)

Set of values: HOME: 1,3,4

col1and col2contain random integers from 1 to 10 for 1000 lines. Then removes any row where col1and col1comprise SAME integer.

I am trying to get these rows from table1, where: col1contains values ​​from MAIN, but col2not. And ALSO col2contains values ​​from MAIN, but col1- no.

In the invented pseudo-code is:

mysql_query("SELECT col1,col2 
             FROM table1 
             WHERE (col1 contains values from MAIN and col2 does not) 
             OR (col2 contains values from MAIN and col1 does not) ");

Any ideas on the right syntax for this?

PS The set of values ​​begins as PHP array.

+3
source share
4 answers

, XOR?

SELECT
  col1,
  col2
FROM
  table1
WHERE
  ( col1 IN ( 1, 3, 4 ) XOR col2 IN ( 1, 3, 4 ) );

, XOR:

SELECT true XOR true; -- false
SELECT true XOR false; -- true
SELECT false XOR true; -- true
SELECT false XOR false; -- false

(col1 in (MAIN) col2 ) OR (col1 (MAIN) col2 (main)), ;)

+4
WHERE (col1 IN (MAIN) AND col2 NOT IN (MAIN)) OR (col1 NOT IN (MAIN) AND col2 IN (MAIN))

MAIN list, of, values, .

PHP , implode(',', $array), IN().

+1

Try the following:

SELECT col1, col2 FROM table1 WHERE (col1 IN MAIN and col2 NOT IN MAIN) OR (col2 IN MAIN AND col1 NOT IN MAIN)
0
source

Sounds like you need something like this

SELECT 
  col1,col2 
FROM 
  table1 
WHERE 
 (col1 in (1,2,3) and col2 not in (1,2,3)) or
 (col1 not in (1,2,3) and col2 in (1,2,3))
0
source

All Articles