AnswerBun.com

from and to ranges in mysql

Currently I have this kind of query

SELECT 
    part_number,
    part_name,
    attenuation_low_end,
    attenuation_high_end,
    optimum_fermentation_temp_f_low,
    optimum_fermentation_temp_f_high
FROM
    yeast_module 
WHERE 
    category = 3
AND
( 
    ( `attenuation_low_end` > '31' OR `attenuation_low_end` = '31' )
    
    AND 
    
    ( `attenuation_high_end` < '40' OR `attenuation_high_end` = '40' )
)

Where I’m trying to get the records with the range of low to high end from 31 and maximum of 40

But it returns me something like this
enter image description here

As you can notice it seems doesn’t return the data between 31 to 40

Am I doing this right?

UPDATE

enter image description here

I’m expecting no return since, there’s no data between 31-40

Stack Overflow Asked by Rae Ian on January 8, 2021

2 Answers

2 Answers

You're comparing strings, which performs lexicographic comparisons rather than numeric comparisons. You need to convert to numbers. Adding 0 to a numeric string is a simple way to convert it to a number.

WHERE 0+attenuation_low_end >= 31 AND 0+attenuation_high_end <= 40

Answered by Barmar on January 8, 2021

If you want ranges contained in the 31-40 range:

where attenuation_low_end >= 31 and attenuation_high_end <= 40

If you want ranges that overlap the 31-40 range:

where attenuation_low_end <= 40 and attenuation_high_end >= 31

If your data is of a string datatype, then you need to convert the values to integers so they can be compared as such.

Containment:

where attenuation_low_end + 0 >= 31 and attenuation_high_end + 0 <= 40

Overlap:

where attenuation_low_end + 0 <= 40 and attenuation_high_end + 0 >= 31

Answered by GMB on January 8, 2021

Add your own answers!

Related Questions

Main Thread Checker Error: Double to UILabel Fails Swift

1  Asked on December 13, 2021 by nisse82

     

Can’t create bitmap without Color space android

1  Asked on December 13, 2021 by iheb-404-notfound

       

fetch id who have register all adv

1  Asked on December 11, 2021 by user13974528

   

Is there a better way to write this snippet in python?

3  Asked on December 11, 2021 by paulo-sergio-schlogl

 

How to set if-else statement in a React function Return()?

4  Asked on December 11, 2021 by buffaurus

   

Check if the next item is 1 unit longer (Javascript)

3  Asked on December 11, 2021 by ekhi-arzac

   

adding onclick to an array of items

5  Asked on December 11, 2021 by helpmepiliizz

       

How to make sure only 1 thread is running? C++

1  Asked on December 11, 2021 by lordi1000

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.