How to declare a variable in mysql, so that my second query can use it?
I would like to write something like:
SET start = 1;
SET finish = 10;
SELECT * FROM places WHERE place BETWEEN start AND finish;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are mainly three types of variables in MySQL:
User-defined variables (prefixed with
@):You can access any user-defined variable without declaring it or
initializing it. If you refer to a variable that has not been
initialized, it has a value of
NULLand a type of string.You can initialize a variable using
SETorSELECTstatement:or
User variables can be assigned a value from a limited set of data
types: integer, decimal, floating-point, binary or nonbinary string,
or NULL value.
User-defined variables are session-specific. That is, a user
variable defined by one client cannot be seen or used by other
clients.
They can be used in
SELECTqueries using Advanced MySQL user variable techniques.Local Variables (no prefix) :
Local variables needs to be declared using
DECLAREbeforeaccessing it.
They can be used as local variables and the input parameters
inside a stored procedure:
If the
DEFAULTclause is missing, the initial value isNULL.The scope of a local variable is the
BEGIN ... ENDblock withinwhich it is declared.
Server System Variables (prefixed with
@@):The MySQL server maintains many system variables configured to a default value.
They can be of type
GLOBAL,SESSIONorBOTH.Global variables affect the overall operation of the server whereas session variables affect its operation for individual client connections.
To see the current values used by a running server, use the
SHOW VARIABLESstatement orSELECT @@var_name.They can be set at server startup using options on the command line or in an option file.
Most of them can be changed dynamically while the server is running using
SET GLOBALorSET SESSION: