SELECT...WHERE...BETWEEN vs SELECT...WHERE...LIKE

A

Anonymous

Guest
To understand this problem, you must realize that "x BETWEEN a AND b" is really a mathematical expression that is equivalent to "x >= a AND x <= b". Now, imagine you have a table like this:

Code:
+-----------+
| name      |
+-----------+
| Albert    |
| Ferdinand |
| Jordan    |
| Mary      |
| Stella    |
| Trogdor   |
+-----------+

If you do the query "SELECT name FROM test1 WHERE name BETWEEN 'F' AND 'S';", what MySQL does is compare each of the names to the values 'F' and 'S'. If it's both greater than 'F' and less than 'S', it will select the value. When it gets to "Stella", it will see that, aphabetically "Stella" comes after, and therefore, it is not BETWEEN 'F' AND 'S', so it will return the following result set:

Code:
mysql> SELECT * FROM test1 WHERE name BETWEEN 'F' AND 'S';
+-----------+
| name      |
+-----------+
| Ferdinand |
| Jordan    |
| Mary      |
+-----------+
3 rows in set (0.00 sec)

Now, if you want to include "Stella", the best way to do it is just use the next letter. So, instead of "BETWEEN 'F' AND 'S'", just do "BETWEEN 'F' AND 'T'". The only place this fails is if you have someone named 'T', but you can easily add a != 'T' clause.
 
I have a bill history of 2 months that have price column
how do i get distinct values so that only months are output
for eg:
i have records from 2002-01-01 to 2003-01-01
how do i get distinct months what only shows me the months .
any ideas :arrow:
 
Back
Top