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
First, let me explain the version of MySQL: mysql...
I haven’t updated my blog for several days. I jus...
MySQL deployment Currently, the company deploys M...
Table of contents Docker Compose usage scenarios ...
Use nginx to configure the separation of PC site ...
This article example shares the specific code of ...
MySQL 8.0.20 installation and configuration super...
closure service nginx stop systemctl stop nginx s...
This article example shares the specific code for...
1. CSS style solves the problem of displaying ell...
docker attach command docker attach [options] 容器w...
Table of contents Preface 1. Preview of office do...
1. useState: Let functional components have state...
A singly linked list can only be traversed from t...
Seeing the recent popular WeChat tap function, I ...