w3resource logo


MySQL not equal to operator

MySQL not equal to (<>, !=) operator

rating Average rating 7 out of 10. Total 33 users rated.

<<PreviousNext>>

Description

MySQL Not equal is used to return a set of rows (from a table) after making sure that two expressions placed on either sides of the NOT EQUAL TO (<>) operator are not equal.

Syntax

<>, !=

MySQL Version : 5.6

Example : MySQL not equal to (<>) operator

The following MySQL statement will fetch the rows from the table publisher which contain publishers those who don't belong to the country USA.

SELECT pub_name,country,pub_city,estd 
FROM publisher 
WHERE country <>"USA";

Sample table : publisher

Output

example mysql not equal to

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-not-equal-operator - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>List of publishers those who don't belong to USA, along with their country, city and date of establishment :</h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='250' align='center'>Publisher</td><td width='250' align='center'>Country</td><td width='250' align='center'>City</td><td width='250' align='center'>Date of establishment</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT pub_name,country,pub_city,estd
FROM publisher
WHERE country <>'USA'");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['pub_name'] . "</td>";
echo "<td align='center' width='200'>" . $row['country'] . "</td>";
echo "<td align='center' width='200'>" . $row['pub_city'] . "</td>";
echo "<td align='center' width='200'>" . $row['estd'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Example : MySQL not equal to (!=) operator with AND using IN operator

The following MySQL statement will fetch the rows from the table book_mast which contain books not written in English and the price of the books are 100 or 200.

SELECT book_name,dt_of_pub,pub_lang,no_page,book_price
FROM book_mast
WHERE pub_lang!="English" AND book_price IN(100,200);

Sample table : book_mast

Output

example1 mysql not equal to

Example : MySQL not equal to ( !=) operator

This following MySQL statement will fetch the rows from the table book_mast which contain books not written in English and the price of the books are less than 100 or more than 200.

SELECT book_name,dt_of_pub,pub_lang,no_page,book_price
FROM book_mast
WHERE pub_lang!="English" AND book_price NOT BETWEEN 100 AND 200; 

Sample table : book_mast

Output

example2 mysql not equal to

Slideshow of MySQL Comparison Function and Operators

MySQL Comparison Function and Operators, slide presentation



We have compiled a number of MySQL Questions and Answers. Check and assess you MySQL skill by visiting those questions and answers.

<<PreviousNext>>