MySQL User-defined variables(UDV)
— mysql — 1 min read
MySQL UDV's are similar to variables used in programs. It can hold only single value. Prefixed with @ before the variable name. You can use UDV without declaring or initializing it. If its not declared or initialized, it will have
MySQL variable assignment
View the stored variable value
At any point in time, it can hold only one value
Where this could be useful ? (Usecases)
- Searching for same value in multiple tables or operations.
- Any type of complex ranking or any counters which you would like to generate.
In the below query,
@rankis getting initialized in the FROM clause like a join and incremented and SET in the SELECT clause