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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:03:53+00:00 2026-06-02T16:03:53+00:00

I have a doubt on how variables work in mysql. As I read in

  • 0

I have a doubt on how variables work in mysql. As I read in their web looks like setting a variable will become visible to the next row.

My table is like:

A     B     C      N
1    NULL   NULL   4
1    NULL   NULL   4 
1     1     NULL   4 
1     1     NULL   4
1     1     1      4
1     1     1      4

What I want is to return only the rows with C = 1. If no rows then return B = 1 and C is NULL if no rows A = 1 and B is NULL and C is NULL.

My idea was:

select N as number,
       @var_c := case when (C = 1) then 1 else -1 end as myc,
       @var_b := case when (@var_c < 0 and B = 1) then 1 else -1 end as myB,
       @var_c := case when (@var_a < 0 and var_b < 0 and C = 1) then 1 else -1 end as myC

from (select @var_a := -1) r_a,
     (select @var_b := -1) r_b,
     (select @var_c := -1) r_c,
     (select A, B, C, N from my_table order by A desc, B desc, C desc) rows

It should (I want to) return

number     myA    myB    myC
  4         -1      -1    1
  4         -1      -1    1
  4         -1      -1    -1
  4         -1      -1    -1
  4         -1      -1    -1
  4         -1      -1    -1

With this and having myA > 0 or myB > 0 or myC > 0 would work.

But it is returning

number    myA     myB    myC
  4         1      -1    -1
  4         1      -1    -1
  4         -1      -1    1
  4         -1      -1    1
  4         -1      1    -1
  4         -1      1    -1

Shouldn’t Mysql keep the vars across the rows?

Regards.

  • 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-02T16:03:54+00:00Added an answer on June 2, 2026 at 4:03 pm

    First of all, do not use variables like this. You shouldn’t expect that you can set a variable in a FROM clause and then access the value in the SELECT clause. That’s not how variables work. Normally you should only use a variable in one place in the query, otherwise you can get undeterministic results.

    Second of all, why don’t you just issue three different queries? First for A = 1 and B is NULL and C is NULL, and if it doesn’t return any rows, issue a query with the second condition set. And so forth.

    And if you ultimately want to issue just a single query, you can try this:

    SELECT N as number
    FROM my_table
    WHERE IF(EXISTS (SELECT A FROM my_table WHERE A = 1 and B is NULL and C is NULL),
        A = 1 and B is NULL and C is NULL,
        IF(EXISTS (SELECT A FROM my_table WHERE B = 1 and C is NULL), B = 1 and C is NULL, C = 1))
    

    But it’s very likely to kill performance. So better just use three queries instead of one.

    UPD: There’s another (yet similar) approach:

    SELECT N as number
    FROM my_table
    WHERE (
      A = 1
      AND B is NULL
      AND C is NULL
    ) OR (
      B = 1
      AND C is NULL
      AND NOT EXISTS (SELECT A FROM my_table WHERE A = 1 and B is NULL and C is NULL)
    ) OR (
      C = 1
      AND NOT EXISTS (SELECT A FROM my_table WHERE A = 1 and B is NULL and C is NULL)
      AND NOT EXISTS (SELECT A FROM my_table WHERE B = 1 and C is NULL)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a doubt concerning declaring variables, their scope, and if their address could
In following program . I have one doubt. I have declared one global variable
I have some doubt about python's class variables. As my understanding, if I define
I have a file like this: 10 15 something I want to read this
I read this site and have a doubt with C. /* * Declare and
A doubt in ASP.NET(VB) I have a public variable in code-behind(ASPX.VB) Public _orderCode As
Possible Duplicate: Objective C Equivalent of PHP's “Variable Variables” I'm studding Objective-C and have
i have one doubt, that is when we create one-to-many as bidirectional. we will
I have doubt when i am reading difference resource for Proxy fetching or No-proxy
Possible Duplicate: What is the difference between const and readonly? i have doubt in

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.