# 变量

在本教程中，您将学习如何在SQL语句中使用MySQL用户定义的变量。

## MySQL用户定义变量简介

有时候，您希望将值从SQL语句传递给另一个SQL语句。为此，您可将该值存储在第一个语句中的MySQL用户定义的变量中，并在随后的语句中引用它。

要创建用户定义的变量，请使用格式`@variable_name`，其中`variable_name`由字母数字字符组成。 用户自定义变量的最大长度为64个字符(*MySQL 5.7.5*之前的版本)

用户定义的变量不区分大小写。 这意味着`@id`和`@ID`是一样的。

可以将用户定义的变量分配给某些数据类型，例如[整数](http://www.yiibai.com/mysql/int.html)，浮点，[小数](http://www.yiibai.com/mysql/decimal.html)，字符串或[NULL](http://www.yiibai.com/mysql/null.html)。

由一个客户端定义的用户定义的变量不被其他客户端看到。 换句话说，用户定义的变量是特定于会话的。

> 注意，用户定义的变量是MySQL特定于SQL标准的扩展。 它们在其他数据库系统中可能不可用。

## MySQL变量赋值

有两种方法可以将值分配给用户定义的变量。

第一种方法是使用`SET`语句如下：

```sql
SET @variable_name := value;
```

您可以使用`:=`或`=`作为`SET`语句中的赋值运算符。 例如，该语句将数字`100`分配给变量`@counter`。

```sql
SET @counter := 100;
```

为变量分配值的第二种方法是使用[SELECT语句](http://www.yiibai.com/mysql/select-statement-query-data.html)。 在这种情况下，您必须使用`:=`赋值运算符，因为在`SELECT`语句中，MySQL将`=`运算符视为相等运算符。

```sql
SELECT @variable_name := value;
```

在赋值之后，可以使用后续语句中允许表达式的变量，例如，在[WHERE子句](http://www.yiibai.com/mysql/where.html)，[INSERT](http://www.yiibai.com/mysql/insert-statement.html)或[UPDATE](http://www.yiibai.com/mysql/update-data.html)语句中。

## MySQL变量示例

以下语句在`products`表中查询获得最昂贵的产品，并将价格分配给用户定义的变量`@msrp`：

```sql
SELECT 
    @msrp:=MAX(msrp)
FROM
    products;
```

执行上面查询语句，得到以下结果 -

```sql
mysql> SELECT 
    @msrp:=MAX(msrp)
FROM
    products;
+------------------+
| @msrp:=MAX(msrp) |
+------------------+
| 214.30           |
+------------------+
1 row in set

mysql> select @msrp as max_price ;
+-----------+
| max_price |
+-----------+
| 214.30    |
+-----------+
1 row in set
```

以下语句使用`@msrp`变量来查询最昂贵产品的信息。

```sql
SELECT 
    productCode, productName, productLine, msrp
FROM
    products
WHERE
    msrp = @msrp;
```

有时候，您想要在表中插入一行，获取最后一个插入ID，并使用将此ID作一项数据插入另一个表。 在这种情况下，您可以使用用户定义的变量来存储[AUTO\_INCREMENT](http://www.yiibai.com/mysql/sequence.html)列生成的最新`ID`，如下所示。

```sql
SELECT @id:=LAST_INSERT_ID();
```

用户定义的变量只能保存一个值。 如果`SELECT`语句返回多个值，则变量将获取结果中最后一行的值。

```sql
SELECT 
    @buyPrice:=buyprice
FROM
    products
WHERE
    buyprice > 95
ORDER BY buyprice;
```

执行上面语句，得到以下结果 -

```sql
+---------------------+
| @buyPrice:=buyprice |
+---------------------+
| 95.34               |
| 95.59               |
| 98.30               |
| 98.58               |
| 101.51              |
| 103.42              |
+---------------------+
6 rows in set
```

接下来，查询上面变量(`@buyprice`)的值，结果如下所示 -

```sql
mysql> SELECT @buyprice;
+-----------+
| @buyprice |
+-----------+
| 103.42    |
+-----------+
1 row in set
```

在本教程中，我们向您展示了如何使用SQL语句中的MySQL变量在会话内的语句之间传递数据。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang-book.gitbook.io/mysql/di-er-zhang/bian-liang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
