![]() Going through the code, we created a function that takes a decimal variable as a parameter and returns a character result. ![]() IF p > 100 then set product_type = 'high range' ĮLSEIF p > 50 then set product_type = 'mid range' Have a closer look at the code.ĬREATE FUNCTION product_category(p DECIMAL) The following code considers the categorization we discussed earlier and employs the IF statement to categorize the product based on the buy price. ![]() Now, we intend to have values in this column based on the values of the buyprice. The above code shows that the column “prod_type” has been successfully added to the table. Let’s load the products table to check if the column has been added properly. And so, first, we need to add it to the table. We don’t have the Product Type as a column in the table. To categorize the products, you should use the following criteria: classify products with a price greater than 100 as “high range,” categorize products with a price greater than 50 but less than or equal to 100 as “mid range,” and classify products with a price less than 50 as “low range.” We intend to categorize the products based on price. Classic Cars, Motorcycles, Planes, Ships, Trucks and Busses, and Vintage Cars. We have a product table containing various details like price, vendor, product types, and product categories for multiple vehicles, viz. The example which we are going to see now demonstrates an out-of-the-box approach. We have to run the function with the SELECT statement to execute it. We validated the result with three sample inputs, shown above, and the screenshots have established the results obtained after the SELECT codes. After checking the conditions, the IF statement concatenates the variables and the statement assigned to the variable “s”. Inside the function, we compare the variables, and based on the condition, we assign appropriate statements to the character variable ‘s’. Note the function returns a string, so we used VARCHAR. We pass two integer values into the function. We explore logic, which is essential though simple. There are two important aspects associated with code: syntax and logic. We evaluated the function over some random values to check each condition and obtained the results below.Īs we can find that the code gave us the right results. Below is the code to implement the function. The function will use the IF statement to generate the correct output. For this, we will create a simple function MAXI in MySQL. In this example, we will find the maximum of two values. Please note that MySQL does not allow an empty block of statements. otherwise, the statement of blocks under ELSE executes. If this condition is satisfied, the block of statements under the ELSEIF statement is executed. If the condition is false, the code moves on to the ELSEIF statement where another condition is evaluated. ![]() If the condition is true, a block of statements is executed. The IF statement evaluates a condition to determine if it is true. Here is the flow diagram for the IF statement in MySQL: Note: The above syntax shows that the statements ELSEIF and ELSE will execute only if the condition in the IF statement is false, as indicated by the square brackets enclosing these statements.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |