Mysql method of assigning query results to variables: 1. Use the "DECLARE variable name type [DEFAULT default value];" statement to create a variable; 2. Use "SELECT field name INTO variable name FROM table name WEHRE query "Condition;" statement assigns the query results to variables.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
Mysql method of assigning query results to variables
1. Create variables
MySQL You can use the DECLARE keyword to define variables. The basic syntax is as follows:
DECLARE 变量名 类型 [DEFAULT 默认值]
If the DEFAULT clause is omitted, the default value is NULL.
Example: Define the variable my_sql, the data type is INT type
DECLARE my_sql INT;
2. Assign the query results to the variable
You need to use SELECT ..INTO
statement, its basic syntax is as follows:
SELECT col_name [...] INTO var_name[,...] FROM table_name WEHRE condition
Among them:
col_name parameter represents the query field name;
SELECT id INTO my_sql FROM tb_student WEHRE id=2;[Related recommendations:
mysql video tutorial]
The above is the detailed content of How to assign query results to variables in mysql. For more information, please follow other related articles on the PHP Chinese website!