
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we create a MySQL stored function that uses the dynamic data from a table?
MySQL Stored functions can reference tables but they cannot make use of statements that return a result set. Hence we can say that there is no SELECT query that returns result set. But we can have SELECT INTO to get rid of that. For example, we are creating a function ‘Avg_marks’ that uses the dynamic data from table named ‘Student_marks’, having following records, to calculate the average of marks.
mysql> Select * from Student_marks; +-------+------+---------+---------+---------+ | Name | Math | English | Science | History | +-------+------+---------+---------+---------+ | Raman | 95 | 89 | 85 | 81 | | Rahul | 90 | 87 | 86 | 81 | +-------+------+---------+---------+---------+ 2 rows in set (0.00 sec) mysql> DELIMITER // mysql> Create Function Avg_marks(S_name Varchar(50)) -> RETURNS INT -> DETERMINISTIC -> BEGIN -> DECLARE M1,M2,M3,M4,avg INT; -> SELECT Math,English,Science,History INTO M1,M2,M3,M4 FROM Student_marks W HERE Name = S_name; -> SET avg = (M1+M2+M3+M4)/4; -> RETURN avg; -> END // Query OK, 0 rows affected (0.01 sec) mysql> DELIMITER ; mysql> Select Avg_marks('Raman') AS 'Raman_Marks'; +-------------+ | Raman_Marks | +-------------+ | 88 | +-------------+ 1 row in set (0.07 sec) mysql> Select Avg_marks('Rahul') AS 'Raman_Marks'; +-------------+ | Raman_Marks | +-------------+ | 86 | +-------------+ 1 row in set (0.00 sec)
- Related Articles
- How can we write MySQL stored procedure to select all the data from a table?
- How can I create a MySQL stored procedure that returns multiple values from a MySQL table?
- How can we delete a MySQL stored function from the database?
- How can we alter a MySQL stored function?
- How can we display all the records from MySQL table with the help of PHP script that uses mysql_fetch_assoc() function?
- How MySQL stored function evaluates if it got NULL value while using the dynamic values from a table?
- How can I create a stored procedure to delete values from a MySQL table?
- How can we create a table from an existing MySQL table in the database?
- How can we use a MySQL stored function in a database query?
- How can we insert data into a MySQL table?
- How to write a MySQL stored function that updates the values in a table?
- How can we export all the data from MySQL table into a text file?
- How can we export all the data from MySQL table into a CSV file?
- How to write a MySQL stored function that inserts values in a table?
- How can we see the source code of a particular MySQL stored function?

Advertisements