Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8311279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:45:31+00:00 2026-06-08T19:45:31+00:00

How to declare a variable in mysql, so that my second query can use

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:45:34+00:00Added an answer on June 8, 2026 at 7:45 pm

    There are mainly three types of variables in MySQL:

    1. 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 NULL and a type of string.

      SELECT @var_any_var_name
      

      You can initialize a variable using SET or SELECT statement:

      SET @start = 1, @finish = 10;    
      

      or

      SELECT @start := 1, @finish := 10;
      
      SELECT * FROM places WHERE place BETWEEN @start AND @finish;
      

      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 SELECT queries using Advanced MySQL user variable techniques.

    2. Local Variables (no prefix) :

      Local variables needs to be declared using DECLARE before
      accessing it.

      They can be used as local variables and the input parameters
      inside a stored procedure:

      DELIMITER //
      
      CREATE PROCEDURE sp_test(var1 INT) 
      BEGIN   
          DECLARE start  INT unsigned DEFAULT 1;  
          DECLARE finish INT unsigned DEFAULT 10;
      
          SELECT  var1, start, finish;
      
          SELECT * FROM places WHERE place BETWEEN start AND finish; 
      END; //
      
      DELIMITER ;
      
      CALL sp_test(5);
      

      If the DEFAULT clause is missing, the initial value is NULL.

      The scope of a local variable is the BEGIN ... END block within
      which it is declared.

    3. Server System Variables (prefixed with @@):

      The MySQL server maintains many system variables configured to a default value.
      They can be of type GLOBAL, SESSION or BOTH.

      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 VARIABLES statement or SELECT @@var_name.

      SHOW VARIABLES LIKE '%wait_timeout%';
      
      SELECT @@sort_buffer_size;
      

      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 GLOBAL or SET SESSION:

      -- Syntax to Set value to a Global variable:
      SET GLOBAL sort_buffer_size=1000000;
      SET @@global.sort_buffer_size=1000000;
      
      -- Syntax to Set value to a Session variable:
      SET sort_buffer_size=1000000;
      SET SESSION sort_buffer_size=1000000;
      SET @@sort_buffer_size=1000000;
      SET @@local.sort_buffer_size=10000;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i declare a global variable and use and modify its value in the function.
When I declare a enum variable like this: enum paint_colors { RED, GREEN, BLUE,
I need to declare 3d array variable but can't. int[][][] ary = new int[5][2][];
I'm trying to declare a global variable from within a class like so: class
Is there a way to declare that the variable type of a generic class
My MySQL table contains a tinyint(1) value that i use to store a true
How can I use dynamic SQL statements in MySQL database and without using session
the MySQL manual says that a CASE statement (the WHEN part) can contain a
I'm (attempting) to write a MySQL stored procedure that parses a large text file.
I need to use a variable to indicate what database to query in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.