Skip to content

LOCAL

CREATE DATABASE test;
USE test;
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(20),
location VARCHAR(20)
);
INSERT INTO employees (id, name, location) VALUES
(1, 'John', 'New York'),
(2, 'Peter', 'Los Angeles'),
(3, 'Suse', 'Boston');
SET @location := 'Los Angeles';
SELECT name, location FROM employees WHERE location = @location;
+-------+-------------+
| name | location |
+-------+-------------+
| Peter | Los Angeles |
+-------+-------------+

In the example, SET @location := 'Los Angeles'; is a declaration of a local variable @location in MySQL. The variable takes the string value ‘Los Angeles’. A SELECT statement is then written to retrieve the names and locations of the employees whose location matches the value of the @location variable. This variable is local to the session in which it is being executed.