Home arrow MySQL arrow Page 4 - Using Boolean Operators for Full Text and Boolean Searches with MySQL

Using the minus operator - MySQL

Implementing full text searches with MySQL can improve the execution of queries against specific database tables. If you want to put this useful feature to work for you, start reading this article now! Welcome to the final tutorial of the series that began with "Performing Full Text and Boolean Searches with MySQL." Made up of three tutorials, this series walks you through the basics of creating full text indexes in MySQL tables, and shows you how to take advantage of Boolean searches to improve the performance of your SQL queries.

  1. Using Boolean Operators for Full Text and Boolean Searches with MySQL
  2. Reintroducing some earlier concepts
  3. Using the plus operator
  4. Using the minus operator
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
June 20, 2007

print this article



As I explained in the previous section, the last example that I'm going to show you in this article surrounding the implementation of Boolean searches with MySQL is based upon using the minus (-) operator. This operator is useful for discarding specific terms from a particular search string.

To demonstrate a simple usage of this handy operator, first I'm going to list the two source files that you learned in the previous section, which look like this:

(definition of form.htm file)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<title>Testing Boolean searches using the minus (-) operator</title>
<style type="text/css">
padding: 0;
margin: 0;
background: #fff;

font: bold 16px Arial, Helvetica, sans-serif;
   color: #000;
text-align: center;

font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
color: #000;

width: 40%;
padding: 10px;
margin-left: auto;
margin-right: auto;
background: #6cf;
<h1>Testing boolean searches using the minus (-) operator</h1>
<div id="formcontainer">
<form action="search.php" method="get">
<p>Enter search term here : <input type="text"
name="searchterm" title="Enter search term here" /><input
type="submit" name="search" value="Search Now!" /></p>

(definition of search.php file)

// define 'MySQL' class
class MySQL{
private $conId;
private $host;
private $user;
private $password;
private $database;
private $result;
const OPTIONS=4;
public function __construct($options=array()){
throw new Exception('Invalid number of connection
foreach($options as $parameter=>$value){
throw new Exception('Invalid parameter '.$parameter);
// connect to MySQL
private function connectDB(){
throw new Exception('Error connecting to the server');
throw new Exception('Error selecting database');
// run query
public function query($query){
throw new Exception('Error performing query '.$query);
return new Result($this,$this->result);
public function escapeString($value){
return mysql_escape_string($value);
// define 'Result' class
class Result {
private $mysql;
private $result;
public function __construct($mysql,$result){
// fetch row
public function fetchRow(){
return mysql_fetch_assoc($this->result);
// count rows
public function countRows(){
return false;
return $rows;
// count affected rows
public function countAffectedRows(){
throw new Exception('Error counting affected rows');
return $rows;
// get ID form last-inserted row
public function getInsertID(){
throw new Exception('Error getting ID');
return $id;
// seek row
public function seekRow($row=0){
throw new Exception('Invalid result set offset');
throw new Exception('Error seeking data');
   // connect to MySQL
$db=new MySQL(array('host'=>'host','user'=>'user','password'=>'password',
$result=$db->query("SELECT firstname FROM users WHERE MATCH
(firstname,lastname,comments) AGAINST('$searchterm' IN BOOLEAN
echo 'No results were found.';
echo '<h2>Users returned are the following:</h2>';
echo '<p>Name: '.$row['firstname'].' Relevance: '.$row
catch(Exception $e){
echo $e->getMessage();

All right, having listed the respective signatures of the above source files, you should notice that the corresponding SELECT query remains completely the same, meaning that the query in question also uses the "IN BOOLEAN MODE" statement.

That being said, here are the results returned by MySQL when the search string "MySQL -database" is entered in the respective web form:

Users returned are the following:

// displays the following entering 'MySQL -database' search term
Users returned are the following:

Name: Alejandro


In this case, the (-) operator has been used to return all the database rows containing the word "MySQL" but not including the term "database." As you can see by the group of basic examples shown here, by using the "IN BOOLEAN MODE" statement it's possible to build quickly a MySQL-driven search engine that supports full text and Boolean searches.

Final thoughts

Sadly, we've come to the end of this series. Hopefully, after examining all of the code samples included in these tutorials, you'll have a much more solid background in how to implement full text and Boolean searches with MySQL.

See you in the next PHP tutorial!

>>> More MySQL Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: