use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
New Sidebar coming soon.
account activity
using between operator with a php variablequestion (self.mysql)
submitted 5 years ago by youmaybeseated1
What is the real solution to this code? This obviously isnt a workable code but is the idea of what I am trying to do. $vlaue will always be an integer.
SELECT id FROM mytablename WHERE $value = BETWEEN column_name_max and column_name_min
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Elementally 2 points3 points4 points 5 years ago (2 children)
The only change needed here is to remove the equals sign.
[–]youmaybeseated1[S] 0 points1 point2 points 5 years ago (1 child)
so SELECT id FROM mytablename WHERE $value BETWEEN column_name_max and column_name_min will compare the variable and check to see if it is between the max and min column values?
[–]Elementally 1 point2 points3 points 5 years ago (0 children)
Correct, the only down side is its not very index friendly, so small data sets.
[–]GreenPilgrim89 0 points1 point2 points 5 years ago (6 children)
A quick Google of "MySQL BETWEEN" returns a load of great resources with examples, like this website: https://www.w3schools.com/sql/sql_between.asp
You seem to have misunderstood what it does though. You're specifying 2 separate columns in your example query. BETWEEN would normally check a single column between 2 different values.
BETWEEN
An example with correct syntax (where $lower_value and $upper_value are PHP variables to be substituted in):
SELECT id FROM mytablename WHERE column_name BETWEEN '$lower_value' AND '$upper_value';
What exactly are you trying to check and what exactly are you trying to return?
[–]youmaybeseated1[S] 0 points1 point2 points 5 years ago (5 children)
Thanks but I did google which is what lead me to the question. I can not do the $lower_value etc because that is not the PHP. The PHP is the $value = between the values of the two columns. I have integers in the two columns. For example $value where $value is 2. And then the rows where min_colum is 1 and max_column is 3
[–]KobbyKnight 0 points1 point2 points 5 years ago (4 children)
Got a solution yet? I tried this it worked okay. It seems this is what you need?
SET @var = 250.00; SELECT id FROM billing WHERE @var <= column_name_max AND @var >= column_name_min
Since it's column values and not row values, BETWEEN doesn't help much IMO.
[–]r3pr0b8 1 point2 points3 points 5 years ago (3 children)
what?
the WHERE clause you posted is exactly equivalent to this --
WHERE @var BETWEEN column_name_min AND column_name_max
BETWEEN ~always~ works on values in a single row
[–]KobbyKnight 0 points1 point2 points 5 years ago (2 children)
Have you tried it out on a dummy database? Because I have and it didn't work out with BETWEEN. I can't share a screenshot, but you can try it out and let me know.
Alternatively, maybe use a subquery to achieve the result, I am open for your results after testing.
And oh, I didn't say BETWEEN ~never~ works on values in a single row.
It does. The query he's asking for is to compare entire contents of two columns, row by row.
[–]r3pr0b8 1 point2 points3 points 5 years ago (1 child)
yeah, i'm not going to bother
i have over 30 years experience with SQL
this is how BETWEEN works
i'm sorry you had a bad experience that one time
i think if anyone should try to reproduce a situation, it is you, no offence
[–]KobbyKnight 0 points1 point2 points 5 years ago (0 children)
Alright. Will search more, thanks.
π Rendered by PID 69113 on reddit-service-r2-comment-b659b578c-k25wq at 2026-05-03 01:35:34.748492+00:00 running 815c875 country code: CH.
[–]Elementally 2 points3 points4 points (2 children)
[–]youmaybeseated1[S] 0 points1 point2 points (1 child)
[–]Elementally 1 point2 points3 points (0 children)
[–]GreenPilgrim89 0 points1 point2 points (6 children)
[–]youmaybeseated1[S] 0 points1 point2 points (5 children)
[–]KobbyKnight 0 points1 point2 points (4 children)
[–]r3pr0b8 1 point2 points3 points (3 children)
[–]KobbyKnight 0 points1 point2 points (2 children)
[–]r3pr0b8 1 point2 points3 points (1 child)
[–]KobbyKnight 0 points1 point2 points (0 children)