SQL AND condition

The SQL AND condition is used in a SQL query to create a SQL statement where records are fetching. It can be used in a SELECT statement, INSERT statement, UPDATE statement or DELETE statement. The SQL AND condition requires that both of conditions should be true.

Let’s see the syntax of AND condition in select statement:

SELECT columns FROM tables  WHERE condition1  AND condition2;  

Let’s see the Example of AND condition in select statement 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, address, salary FROM employee WHERE department=’teacher’ AND age>=’24’”);
?>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Address</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[‘address’];?></td>
<td><?php echo $result[‘salary’];?></td>
</tr>  
<?php
} 
?>
<table>

Output will be look like that :

IDNAMEAGEADDRESSSALARY
1Jeevan gupta30jaipur15000
2Suraj tiwari30bikaner17000
3Papu shing30kota24000

Let’s see the Example of AND condition in UPDATE statement:

You can see in example that how an SQL “AND” condition is used in SQL UPDATE statement

<?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()); 
mysql_query(“UPDATE employee set salary=’30000’ WHERE salary=’24000’ AND age>=’24’”);
?>

Let’s see the Example of AND condition in DELETE statement:

You can see in example that how an SQL “AND” condition is used in SQL DELETE statement

<?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(“DELETE from employee WHERE salary=’24000’ AND age>=’24’”);
?>

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.