Home >Database >Mysql Tutorial >How to set environment variables in mysql

How to set environment variables in mysql

下次还敢
下次还敢Original
2024-04-14 18:45:32524browse

There are two ways to set environment variables in MySQL: use the SET command: set the environment variable name to the value, and the effective scope is the entire session and all subsequent sessions. Use the SESSION command: Set the environment variable name to a value, which only takes effect in the current session, and clears the variable after the session ends.

How to set environment variables in mysql

How to set environment variables in MySQL

There are two ways to set environment variables in MySQL:

Method 1: Use the SET command

<code>SET 环境变量名 = 值;</code>

For example:

<code>SET @user = 'john';</code>

This command sets the environment variable @user to the value" john".

Method 2: Use the SESSION command

<code>SET SESSION 环境变量名 = 值;</code>

is similar to the SET command, but the environment variables set by the SESSION command are only Valid in the current session. After the session ends, the variables will be cleared.

For example:

<code>SET SESSION @temp_dir = '/tmp';</code>

Using environment variables

After setting environment variables, you can use them to reference variable values.

<code>SELECT * FROM users WHERE username = @user;</code>

Note:

  • Environment variable names must start with the '@' symbol.
  • The type of environment variable value can be string, number or Boolean value.
  • Environment variables can be used like other columns in SQL statements.

The above is the detailed content of How to set environment variables in mysql. For more information, please follow other related articles on the PHP Chinese website!

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