In this post we will write a function that sums two number and returns result. Then, we will use this function in various queries. The function is below. First, we must execute this function. After get succesful message then we can do examples about it.
1 2 3 4 5 6 7 8 9 10 |
DELIMITER $$ CREATE FUNCTION `fn_AddTwoNumber`( `n1` INT, `n2` INT ) RETURNS INT BEGIN RETURN n1+n2; END$$ DELIMITER ; |
EXAMPLES
Example 1 Add 3 to 5 with fn_AddTwoNumber
1 |
SELECT fn_AddTwoNumber(3, 5) AS Result |
Example 2 Add the pageCount value to the Point value from books table and list the values
1 |
SELECT pagecount,POINT, fn_AddTwoNumber(pagecount, point) AS Total FROM books |
Example 3 Add 10 point to to the books which the typeId is 3
1 |
SELECT *, fn_AddTwoNumber(POINT, 10) AS newPoint FROM books WHERE typeId = 3 |