How to Introduce Multiple Conditions in Like Operator

How can I introduce multiple conditions in LIKE operator?

Here is an alternative way:

select * from tbl where col like 'ABC%'
union
select * from tbl where col like 'XYZ%'
union
select * from tbl where col like 'PQR%';

Here is the test code to verify:

create table tbl (col varchar(255));
insert into tbl (col) values ('ABCDEFG'), ('HIJKLMNO'), ('PQRSTUVW'), ('XYZ');
select * from tbl where col like 'ABC%'
union
select * from tbl where col like 'XYZ%'
union
select * from tbl where col like 'PQR%';
+----------+
| col |
+----------+
| ABCDEFG |
| XYZ |
| PQRSTUVW |
+----------+
3 rows in set (0.00 sec)

Can the NOT operator work on multiple conditions present in parenthesis?

You got bitten by operator priority. Parentheses are your friend, you see.

WHERE NOT Country='Germany' AND NOT Country='USA';

actually means, since NOT has precedence over AND:

WHERE (NOT Country='Germany') AND (NOT Country='USA');

Since "NOT a=b" is "a!=b", this means:

WHERE Country!='Germany' AND Country!='USA';

which, as the others pointed out, looks muuuuch better like this:

WHERE Country NOT IN ('Germany', 'USA');

Now, next topic. This one:

WHERE NOT (Country='Germany' AND Country='USA');

Country can not be Germany and USA at the same time, so the stuff inside the () is always FALSE, so this equates to:

WHERE NOT (FALSE);

i.e.

WHERE TRUE;

ie, you get your whole table.



Related Topics



Leave a reply



Submit