Home  >  Article  >  Database  >  mysql variable declaration and assignment

mysql variable declaration and assignment

黄舟
黄舟Original
2016-12-16 14:06:301218browse

Variables in sql server must be declared first and then assigned a value:

Local variables are marked with one @, and global variables are marked with two @s (commonly used global variables are generally already defined);

The syntax for declaring local variables: declare @variable Name data type; for example: declare @num int;

Assignment: There are two methods (@num is the variable name, value is the value)

set @num=value; or select @num=value;

If you want To obtain a field value in a query statement, you can use select to assign a value to a variable, as follows:

select @num=Field name from table name where...

Variables in MySQL do not need to be declared in advance. When using them, use "@variable name" directly. "Just use it.

First usage: set @num=1; or set @num:=1; //Variables are used to save data here, use @num variables directly

Second usage: select @num:=1; Or select @num:=field name from table name where...

Pay attention to the above two assignment symbols. You can use "=" or ":=" when using set, but you must use ":= assignment" when using select

The above is the content of mysql variable declaration and value assignment. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn