Does anyone know a quick way to query a range of data from MySQL database? For example, I want to query any person who have a last name starting with M to P or to query using a range of years such as from 1980 to 2000 . Is there a quick and easy SQL statement to do this?
Your suggestion is much appreciated.
ljCharlie
How to query a range of data from MySQL?
Moderator: General Moderators
BETWEEN might help: http://dev.mysql.com/doc/mysql/en/Compa ... ators.html
As far as a person with a last name, look into using REGEX, such as:
As far as a person with a last name, look into using REGEX, such as:
Code: Select all
SELECT first_name, last_name FROM people WHERE last_name REGEXP '^їm-p]'Sorry, forgot the link: http://dev.mysql.com/doc/mysql/en/Strin ... tions.html
Thanks! I'll give that try.
By the way, if I'm querying from 2004-04-01 to 2004-04-30 and the field I have in the database is declared as DATE which has this format yyyy-mm-dd, do you think the BETWEEN will work if I do this:
їcode]$query = 'SELECT * FROM regUsers WHERE myDate BETWEEN "'.$dateFrom.'" AND "'.$dateTo.'"';ї/code]
ljCharlie
By the way, if I'm querying from 2004-04-01 to 2004-04-30 and the field I have in the database is declared as DATE which has this format yyyy-mm-dd, do you think the BETWEEN will work if I do this:
їcode]$query = 'SELECT * FROM regUsers WHERE myDate BETWEEN "'.$dateFrom.'" AND "'.$dateTo.'"';ї/code]
ljCharlie
There is a lot of string functions in php, regexp is perhaps slowest among them. for you particular case (checking prefix of the name):
is as much as two times faster then regexp.
Code: Select all
select * from sometable where substring(fname,0,1) in ('m','n','o','p');