How to use If-Else in Mysql

If else is basically used in the programming paradigm. We can use the same in Mysql.

How to use If-Else Mysql

Sometime it is very useful in programming. Suppose you have two column in the table as price and discountprice in the product column. You want to fetch the price and show it in the front end. If the discount is set in the table then it will be the actual price not the value that is set in the price column.

Lets take a record in the table,

Name Price discountprice
ABC 2300 2100
XYZ 2900

I want to show the discountprice as price if its value is there on the column as in the case of “ABC”
but the discountprice don’t have any value for the product XYZ .
Through programming we can check the discountprice and price column and will show the price according to it.
In PHP we can write the above as

$sql=mysql_query(“SELECT * FROM product”);

while($record=mysql_fetch_assoc($sql)){
if($record[‘discountprice ‘])
echo “Price : “.$record[‘discountprice ‘];
else
echo “Price :”.$record[‘price ‘];
}

Instead of applying logic in server side language like PHP, we can apply the login at query which is very correct approach also. the query will be like this.
SELECT IF(discountprice

From above query price will always have the minimium price out of the price and discountprice.

Read More Post

STAY CONNECTED

Most Popular

Manifesting Your Dream Career: Finding Your Passion And Purpose
01 Aug

Manifesting Your Dream Career: Finding Your Passion And Purpose

Are you passionate about finding your dream career? Manifesting can help you align your career with your passions and purpose.

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service
27 Feb

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service

Finding a reliable auto service provider is paramount in the bustling world of vehicle maintenance and repair. That's where Carolina

Floratam: The Robust Grass Solution From Council Growers SOD
25 Jan

Floratam: The Robust Grass Solution From Council Growers SOD

Floratam grass, a popular St. Augustine variety, has become a go-to choice for homeowners and landscapers seeking a lush, durable

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair
25 Jan

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair

For vehicle owners, the braking system is among the most critical components for safety and performance. Carolina Mobile Autoservice, a

Safeguarding Your Property: Expert Water Damage Services By MBC Capital
24 Jan

Safeguarding Your Property: Expert Water Damage Services By MBC Capital

Water damage can significantly impact homes and businesses, often leaving a trail of destruction and disruption in its wake. Understanding

Revanesse Lip Filler: The Secret To A Perfect Pout – Discover America’s New Favorite At Beauty From Ashes Aesthetics
19 Jan

Revanesse Lip Filler: The Secret To A Perfect Pout – Discover America’s New Favorite At Beauty From Ashes Aesthetics

In the ever-evolving world of cosmetic enhancements, there's a constant quest for the next big thing. The latest darling in