I would like to create a query that filters records in a table using a date range .
I have this existing function that filters the records by "type" automatically, but how can I attach arguments to this function that uses a date range to only show records that fall within the range?
Say, for example, find a record that falls between 04/01/2015 and 04/30/2015.
public function getByType() {
$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` WHERE type='hod'");
$query->execute();
if ($query->rowCount() == 0) return null;
}
Thank you!
You are storing dates with wrong data types. Storing dates as varchar string is like inviting evil to your application. You should always store dates with mysql native date data types date,datetime,timestamp
etc. with the date as Ymd
format
However in the current case you can use str_to_date
function to get the job done, but in long run you should think about correcting these.
So here how it works
mysql> select str_to_date('04/01/2015','%m/%d/%Y') as d ;
+------------+
| d |
+------------+
| 2015-04-01 |
+------------+
So the query you could use as
SELECT * FROM table_name
WHERE
type='hod'
and str_to_date(date,'%m/%d/%Y') between str_to_date('04/01/2015','%m/%d/%Y')
and str_to_date('04/30/2015','%m/%d/%Y')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.