WHERE provisions channel different articulations, for example, SELECT, UPDATE, DELETE, and INSERT. They present rules used to determine activity. They regularly show up after a table name in an articulation, and their condition follows. The WHERE provision basically works like an if explanation.
Audit the overall punctuation of WHERE condition given beneath −
[COMMAND] field,field2,... FROM table_name,table_name2,... WHERE [CONDITION]
Note the accompanying characteristics of the WHERE condition −
- It is discretionary.
- It permits any condition to be determined.
- It takes into account the determination of numerous conditions through utilizing an AND OR administrator.
- Case affectability just applies to explanations utilizing LIKE examinations.
The WHERE condition allows the utilization of the accompanying administrators −
Operator |
---|
= != |
> < |
>= <= |
WHERE conditions can be used at the order brief or inside a PHP content.
The Command Prompt
At the order brief, basically utilize a standard order −
root@host# mysql -u root -p password;
Enter password:*******
mysql> use PRODUCTS;
Database changed
mysql> SELECT * from products_tbl WHERE product_manufacturer = 'XYZ Corp';
+-------------+----------------+----------------------+
| ID_number | Nomenclature | product_manufacturer |
+-------------+----------------+----------------------+
| 12345 | Orbitron 4000 | XYZ Corp |
+-------------+----------------+----------------------+
| 12346 | Orbitron 3000 | XYZ Corp |
+-------------+----------------+----------------------+
| 12347 | Orbitron 1000 | XYZ Corp |
+-------------+----------------+----------------------+
Audit a model utilizing the AND condition −
SELECT *
FROM products_tbl
WHERE product_name = 'Bun Janshu 3000';
AND product_id <= 344;
This model joins both and additionally conditions
SELECT *
FROM products_tbl
WHERE (product_name = 'Bun Janshu 3000' AND product_id < 344)
OR (product_name = 'Bun Janshu 3000');
PHP Scripts Using Where Clause
Utilize the mysql_query() work in activities utilizing a WHERE proviso −
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT product_id, product_name, product_manufacturer, ship_date
FROM products_tbl
WHERE product_manufacturer = "XYZ Corp"';
mysql_select_db('PRODUCTS');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Product ID :{$row['product_id']} <br> ".
"Name: {$row['product_name']} <br> ".
"Manufacturer: {$row['product_manufacturer']} <br> ".
"Ship Date: {$row['ship_date']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
On effective information recovery, you will see the accompanying yield −
Product ID: 12345
Nomenclature: Orbitron 4000
Manufacturer: XYZ Corp
Ship Date: 01/01/17
----------------------------------------------
Product ID: 12346
Nomenclature: Orbitron 3000
Manufacturer: XYZ Corp
Ship Date: 01/02/17
----------------------------------------------
Product ID: 12347
Nomenclature: Orbitron 1000
Manufacturer: XYZ Corp
Ship Date: 01/02/17
----------------------------------------------
mysql> Fetched data successfully
