MySQL WHERE Clause

MySQL WHERE Clause

We have seen SQL SELECT command to fetch data from MySQL table, UPDATE command for update data into mysql table, DELETE data from mysql table. We can use a conditional clause called WHERE clause to filter out results. Using WHERE clause, we can specify a selection criteria to select required records from a table.
WHERE clauses are not mandatory clauses of SQL DML statements. But it can be used to limit the number of rows affected by a SQL DML statement or returned by a query.
WHERE clause does not use only with SELECT statement it is used in SELECT, UPDATE, DELETE statement etc.

Let’s see the syntax for sql where clause:

  SELECT column1, column 2, ... column n  FROM    table_name  WHERE [conditions] ;

1. You can use one or more tables separated by comma to include various conditions using a WHERE clause, but WHERE clause is an optional part of SELECT command and rest of other commands.
You can specify any condition using WHERE clause.
2. You can specify more than one conditions using AND or OR operators.
3. The WHERE clause works such as if condition in any programming language. This clause is used to compare given value in mysql condition with the field value available in MySQL table. If given value from outside is match to the available field value in MySQL table acording to given condition, then it returns that row.

Here we are explaining the list of operators, which can be used with WHERE clause.

OperatorDescriptionExample
== Operator checks if the values of two operands are equal or not, if yes then condition return will be true.(A = B) is not true.
!=!= Operator checks if the values of two operands are equal or not, if values are not equal then condition return will be true.(A != B) is true.
>> Operator checks if the value of left operand is greater than the value of right operand, if yes then condition return will be true.(A > B) is not true.
<< Operator checks if the value of left operand is less than the value of right operand, if yes then condition return will be true.(A < B) is true.
>=>= Operator checks if the value of left operand is greater than
or equal to the value of right operand, if yes then condition return will be
true.
(A >= B) is not true.
<=<= Operator checks if the value of left operand is less than or equal to the value of right operand, if yes then condition return will be true.(A <= B) is true.

Fetching Data Using WHERE CLAUSE with PHP script

You can use same SQL SELECT command with WHERE CLAUSE into PHP function mysql_query(). This function is used to execute SQL command and mysql_fetch_assoc() can be used to fetch all the selected data. This function returns row as an associative array, this function is faster then the mysql_fetch_array() function. This function returns FALSE if there are no more rows.

Let’s see the Example for sql where clause with PHP script:

<?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error()); 
$sql = mysql_query(“SELECT id, name, age, salary FROM employee WHERE salary>=’15000’”);
?>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Salary</th>
</tr>
<?php
while($result = mysql_fetch_assoc($sql))
{?> 
<tr>
<td><?php echo $result[‘id’];?></td>
<td><?php echo $result[‘name’];?></td>
<td><?php echo $result[‘age’];?></td>
<td><?php echo $result[‘salary’];?></td>
</tr>
<?php
} 
?>
<table>

Output will be look like that:

IdNameAgeSalary
4Nitesh Kumar2920000
8Rahul Choudhary2225000
9Roshan2623000

Leave a Reply

Related Posts: You may like

comma separated values in mysql query

SQL statement to retrieve names beginning with A

Php pdo prepared statements select insert update queries

php pdo queries statements select insert update delete

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.