Skip to content
Advertisement

How to compare a value within a range, inside (case when ~~ end as) query for mysql?

this is what I got:

and I want to change this part as,

for now I get the raw data of rssi, for example any digits between 0 and 120..

but now I want to change the rssi result if the data is in between certain range.

For example, if rssi is between 0 and 20, its 1. if its between 21 and 40, its 2, kinda.

I have tried:

which told me I have mysql error. Also I have tried:

but also errored.

I’m pretty lost at how to compare my rssi within a range. Because it’s inside the case (when~~else end as) query. Can anyone help me?

Advertisement

Answer

I think that you want:

Rationale:

  • case expects a series of when ... then ... constructs and a single else
  • you need ifnull(rssi,0) only when 0 is in the control range (otherwise, if it is null, it will not match anyway)
  • the column alias must be present only once, at the end of the outer case
  • returning numeric values would probably make more sense than strings
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement