How to declare a variable in MySQL?(如何在 MySQL 中声明一个变量?)
问题描述
如何在 mysql 中声明一个变量,以便我的第二个查询可以使用它?
How to declare a variable in mysql, so that my second query can use it?
我想写一些类似的东西:
I would like to write something like:
SET start = 1;
SET finish = 10;
SELECT * FROM places WHERE place BETWEEN start AND finish;
推荐答案
MySQL中主要有以下三种变量:
There are mainly three types of variables in MySQL:
用户定义的变量(前缀为
@
):
User-defined variables (prefixed with
@
):
您可以访问任何用户定义的变量而无需声明或初始化它.如果你引用了一个没有被引用的变量初始化后,它的值为 NULL
和字符串类型.
You can access any user-defined variable without declaring it or
initializing it. If you refer to a variable that has not been
initialized, it has a value of NULL
and a type of string.
SELECT @var_any_var_name
您可以使用 SET
或 SELECT
语句初始化变量:
You can initialize a variable using SET
or SELECT
statement:
SET @start = 1, @finish = 10;
或
SELECT @start := 1, @finish := 10;
SELECT * FROM places WHERE place BETWEEN @start AND @finish;
可以从一组有限的数据中为用户变量赋值类型:整数、十进制、浮点、二进制或非二进制字符串,或 NULL 值.
User variables can be assigned a value from a limited set of data types: integer, decimal, floating-point, binary or nonbinary string, or NULL value.
用户定义的变量是特定于会话的.也就是说,一个用户一个客户端定义的变量不能被其他客户端看到或使用客户.
User-defined variables are session-specific. That is, a user variable defined by one client cannot be seen or used by other clients.
它们可以在使用 SELECT 查询中使用/">高级 MySQL 用户变量技术.
They can be used in SELECT
queries using Advanced MySQL user variable techniques.
局部变量(无前缀) :
Local Variables (no prefix) :
局部变量需要先用DECLARE
声明访问它.
Local variables needs to be declared using DECLARE
before
accessing it.
它们可以用作局部变量和输入参数在存储过程中:
They can be used as local variables and the input parameters inside a stored procedure:
DELIMITER //
CREATE PROCEDURE sp_test(var1 INT)
BEGIN
DECLARE start INT unsigned DEFAULT 1;
DECLARE finish INT unsigned DEFAULT 10;
SELECT var1, start, finish;
SELECT * FROM places WHERE place BETWEEN start AND finish;
END; //
DELIMITER ;
CALL sp_test(5);
如果 DEFAULT
子句缺失,初始值为 NULL
.
If the DEFAULT
clause is missing, the initial value is NULL
.
局部变量的作用域是 BEGIN ... END
块内它被声明.
The scope of a local variable is the BEGIN ... END
block within
which it is declared.
服务器系统变量(前缀使用 @@
):
Server System Variables (prefixed with @@
):
MySQL 服务器维护了许多配置为默认值的系统变量价值.它们可以是 GLOBAL
、SESSION
或 BOTH
类型.
The MySQL server maintains many system variables configured to a default value.
They can be of type GLOBAL
, SESSION
or BOTH
.
全局变量影响服务器的整体操作,而会话变量影响其对单个客户端连接的操作.
Global variables affect the overall operation of the server whereas session variables affect its operation for individual client connections.
要查看正在运行的服务器使用的当前值,请使用 SHOW VARIABLES
语句或 SELECT @@var_name
.
To see the current values used by a running server, use the SHOW VARIABLES
statement or SELECT @@var_name
.
SHOW VARIABLES LIKE '%wait_timeout%';
SELECT @@sort_buffer_size;
它们可以在服务器启动时使用命令行或选项文件中的选项进行设置.大多数可以在服务器运行时使用 SET GLOBAL
或 SET SESSION
动态更改:
They can be set at server startup using options on the command line or in an option file.
Most of them can be changed dynamically while the server is running using SET GLOBAL
or SET SESSION
:
-- Syntax to Set value to a Global variable:
SET GLOBAL sort_buffer_size=1000000;
SET @@global.sort_buffer_size=1000000;
-- Syntax to Set value to a Session variable:
SET sort_buffer_size=1000000;
SET SESSION sort_buffer_size=1000000;
SET @@sort_buffer_size=1000000;
SET @@local.sort_buffer_size=10000;
这篇关于如何在 MySQL 中声明一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 MySQL 中声明一个变量?
基础教程推荐
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01