Skip to content

Instantly share code, notes, and snippets.

@gbarreiro
Created August 20, 2020 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbarreiro/a039c1f27d0279c0bbe5a6e795440c54 to your computer and use it in GitHub Desktop.
Save gbarreiro/a039c1f27d0279c0bbe5a6e795440c54 to your computer and use it in GitHub Desktop.
MySQL cheatsheet: variables
-- Local variables (must be declared inside a STORED PROCEDURE)
DECLARE john_age INT; -- defines the variable
SET john_age = 21; -- sets a hard-coded value to a variable
SELECT age INTO jonh_age FROM Students WHERE id = 10; -- stores the result of a query into a variable
-- Session (user-defined) variables
SET @current_age = 21; -- we don't need to define it before
SELECT age INTO @current_age FROM Students WHERE id = 10; -- stores the result of a query into a session variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment