SQL OR condition

The SQL OR 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 OR condition requires that one of them conditions should be true.

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

  SELECT columns FROM tables  WHERE condition1  OR condition2;

Let’s see the Example of OR 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’ OR 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
1himesh gupta24jaipur15000
2Shivani tiwari23bikaner17000
3puran shing28kota24000
4Gulshan kumar15delhi18000

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

You can see in example that how an SQL “OR” 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’ OR age>=’24’”);
?>

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

You can see in example that how an SQL “OR” 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()); 
mysql_query(“DELETE from employee WHERE salary=’24000’ OR 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.