A MySQL custom value is a temporary container for storing values. As long as the connection to the server is active, the values in the container can be saved and used. Custom variables can be set with a simple SET or SELECT statement, as follows: SET @one := 1; SET @min_actor := (SELECT MIN(actor_id) FROM sakila.actor); SET @last_week := CURRENT_DATE-INTERNAL 1 WEEK; After defining the variable, you can use it in SQL statements: SELECT * FROM film WHERE created_date <= @last_week; Although MySQL custom variables are very powerful, you also need to know that custom variables themselves have flaws, including:
Custom variables can be used in all types of statements, not just SELECT statements. In fact, this is one of the biggest advantages of custom variables. For example, we can rewrite complex queries to use subqueries to perform sort calculations, or to perform a low-cost UPDATE statement. Sometimes, the optimizer will think that the variable is a compile-time constant and not assign a value to it, which can cause expected behavior to be strange. Putting custom variable assignments into a function like LEAST usually avoids this problem. Another way is to check if the custom variable has a value before using it. Sometimes you want to do it, and sometimes you don't. With a little experimentation, we can do a lot of interesting things with custom variables, such as:
The above is the detailed content of the concept and characteristics of MySQL custom variables. For more information about MySQL custom variables, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Use tomcat to deploy SpringBoot war package in centos environment
>>: Methods and techniques for quickly displaying web page images
Optimize the fastcgi configuration file fcgiext.i...
It is often necessary to run commands with sudo i...
From the tomcat configuration file, we can see th...
Table of contents 1. Installation 2. There is no ...
Effect: Ideas: Use the input type attribute to di...
This article shares the specific code of uni-app ...
Data Sheet /* Navicat SQLite Data Transfer Source...
1. Prepare the Docker environment 2. Search for f...
Before you begin Have a cloud server, mine is Ten...
Project Background Recently, there is a project w...
Solution to the data asymmetry problem between My...
Table of contents 01 Introduction to GTID 02 How ...
Step 1: Enter the directory: cd /etc/mysql, view ...
I have always wanted to learn about caching. Afte...
What is MySQL multi-instance Simply put, MySQL mu...