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

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

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.