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 6564255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:56:36+00:00 2026-05-25T13:56:36+00:00

Consider the query: SELECT @xxx As thevar FROM (SELECT @xxx:=1) as temp Will the

  • 0

Consider the query:

SELECT @xxx As thevar FROM (SELECT @xxx:=1) as temp

Will the resulting query always yield 1, even if there are many other similar queries running at the same time (or as close to the same time as they can get) that are setting @xxx to a different integer.

Also, what exactly is the scope of @xxx?

I’m trying to determine if this is a safe (albeit hacky) way to insure a variable is set to the value I want for the life a specific query.

These queries are run against a single instance of MySQL 5.5 and may be done on the same connection or different connections to the database.

  • 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-05-25T13:56:37+00:00Added an answer on May 25, 2026 at 1:56 pm

    From what I have just tested, it seems the variable is seen only on the current connection. Or more precisely on the connection on which it is set.

    I believe your SQL Statement has the same effect as the SET statement for User Defined Variables.

    The documentation for which states that:

    User-defined variables are connection-specific. That is, a user variable defined by one client cannot be seen or used by other clients. All variables for a given client connection are automatically freed when that client exits.

    Thus if the other queries are using there own separate connections, than they will have their own copies of @xxx, and will not interfere with each other. And your hack will work as expected. But if same connection is used and the order of query execution cannot be guaranteed then, other queries can change @xxx and affect the subsequent queries.

    Regarding if this always return 1:

    SELECT @xxx As thevar FROM (SELECT @xxx:=1) as temp
    

    It should, BUT if using the same connection there is an outlier chance that a Seperate Thread using the same connection executes SET @xxx=2 just after the execution of the subquery and before outer select. Please correct me if I’m wrong about ATOMICITY of SELECT here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider an SQL query like this SELECT Temp.rating, Temp.avgage FROM (SELECT S.rating, AVG (S.age)
Consider the following query: SELECT operation, [...] FROM tableName The operation field will retrieve
Consider I have such SQL query: SELECT t1.* FROM table1 t1 LEFT JOIN table2
Consider this query: SELECT table1.id, table1.review, table1.time, table2.author, table2.title FROM table1, table2 WHERE table1.id
Consider the SQL query below: SELECT DISTINCT shops.*, DA.delivery_cost, DA.postcode FROM shops JOIN shops_delivery_area
Consider the SQL query below: SELECT * FROM opening_hours WHERE week_day = WEEKDAY(NOW()) +
Consider a simple query: SELECT id, date, hours FROM employees where id=10 And the
Consider the following SQL Server 2005/2008 query: Select [UID], [DESC] From SomeTable Order By
Let's consider such query: SELECT * FROM ( SELECT YEAR(OrderDate) [Year], MONTH(OrderDate) [Month], SubTotal
Consider the following JPQL query: SELECT foo FROM Foo foo INNER JOIN FETCH foo.bar

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.