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

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.