It's a little temporary today. To calculate the results of a table, do not want to write code, think of the MySQL custom function. Encountered a lot of problems, in order to facilitate the use of the record here.
Requirements: In a table, there are scores, you need to check out the match ID and match results. Analysis: Single-table query nothing, difficulties in how to judge the score after the return of the desired results.
Here I define the match results separately, 1 home win, 2 draw, 3 away win
Function logic:
receive two parameters, determine the size of the parameters, and return the results respectively.
Implementation: Navicat Operation: navicat for MySQL tool--create function
Here are the stored procedures and functions, we select the function
Set the parameter, where the parameter is the received parameter, such as COUNT (ID) to receive a parameter
Next, set the return type
Click Done. The following interface will appear
Now begins at begin. END;; The middle write logic, the specific grammar needs Baidu.
I am mainly if...elseif here. Else's logic, the code is as follows:
Save--getmatchresult. If the syntax is wrong, save will error, according to the prompt information to find the error.
using functions:
For convenience, I have written two parameters directly, with the same results as expected. The first parameter is the home score, the second argument is the away score,3 means away win.
MySQL creates a function into the MySQL shell because there are many semicolons in the middle, so you need to use the delimiter demarcation. The command indicates that the default semicolon execution is changed to the other symbol execution.
Here to use Demiter to modify the decomposition character//, if the use of the semicolon demarcation is an error. After the use//demarcation is performed, the display is normal.
Create a function (note using//demarcation)
using Functions
The semicolon ends with no effect and must be used//unless you log out of the MySQL shell again. The return result is what we need, and the 2nd parameter is away. 3 means away win.
To this simple trial completed, the temporary query is very convenient.
From for notes (Wiz)
How MySQL functions are used