Connecting Tech Pros Worldwide Help | Site Map

MySQL Logical Operators

By Blair Ireland
Senior Editor, TheScripts.com

Logical Operators

One of the great features within MySQL is its full support for logical operations. I will name off, and show examples of them below

mysql> CREATE TABLE users (
-> id INT NOT NULL AUTO_INCREMENT,
-> name VARCHAR (50),
-> email VARCHAR (50),
-> PRIMARY KEY (id));

NOT (or) !

mysql> SELECT * FROM users WHERE
-> (name != "Blair Ireland");

or

mysql> SELECT * FROM users WHERE
-> (name NOT = "Blair Ireland");

This query would return all records without Blair Ireland present as the name.

AND (or) &&

mysql> SELECT * FROM users WHERE
mysql> (name = "Blair Ireland") AND mysql> (email = "bireland@domainname.com");

or

mysql> SELECT * FROM users WHERE
-> (name = "Blair Ireland") &&
-> (email = "bireland@domainname.com");

This query would return all records with Blair Ireland present as the name, and bireland@domainname.com as the email.

OR ( or ) ||

mysql> SELECT * FROM test WHERE
-> (name = "Blair Ireland") OR
-> (email = "bireland@domainname.com");

or

mysql> SELECT * FROM test WHERE
-> (name = "Blair Ireland") ||
-> (email = "bireland@domainname.com");

This query would return all records with Blair Ireland present as the name, or records with bireland@domainname.com as the email.

« Using MySQL Manipulating Tables »