Create an 'inverse' selection from a mysql query -
i have long , complex mysql query on table lot of 'where fieldx '%abc%'' , few 'where fieldx not '%efg%'. without doing bunch of search , replace change not , vice-versa, there simple query can select records not meet large query parameters vs ones designed do?
simple example:
- i have tablea field called
letter
26 records, 1 each letter of alphabet.
i have query says:
select * tablea `letter` = 'a' or `letter` = 'b' or `letter` = 'c' or or `letter` = 'd' or `letter` = 'e' or `letter` = 'f' or `letter` not 'g' or `letter` not 'h'
now instead of doing search , replace reverse/negate of 'where' conditions:
select * tablea `letter` != 'a' or `letter` != 'b' or `letter` != 'c' or or `letter` != 'd' or `letter` != 'e' or `letter` != 'f' or `letter` 'g' or `letter` 'h'
i want use existing query , same result. thought there might way add select or other modifier select records ones in original select:
select * tablea `letter` != (select * tablea `letter` != 'a' or `letter` != 'b' or `letter` != 'c' or or `letter` != 'd' or `letter` != 'e' or `letter` != 'f' or `letter` not 'g' or `letter` not 'h')
cleary not sure if last example working mysql goal; encase original query inside end returning of record not selected in original query.
first of all, when inverting comparisons, have change or
and
, otherwise every record in table. either not 'a' or not 'b'. :)
it should be:
select * tablea `letter` != 'a' , `letter` != 'b' , ...
back question, think easiest solution enclose entire condition in parentheses , add not
operator:
select * tablea not (`letter` = 'a' or `letter` = 'b' or ....)
Comments
Post a Comment