The database has a table with currency rates and fields with a timestamp, but data is collected several times on one day, so there are many duplicates, how can I write a request to select data with grouping by date, not date-time, for example, for the last year
3 answers
You can use the GROUP BY operator, on a calendar field, which is passed through the MySQL function DATE()
SELECT * FROM tbl GROUP BY DATE(created_at) However, it is best to get rid of duplicates by simply adding a unique index for the date field and using the IGNORE keyword in the INSERT to ignore duplicate inserts.
- And why did you decide that several entries per day is a mistake? The course can actually change several times a day. - artoodetoo
- Because I take it from the Central Bank site once a day, I am not interested in dynamic - Anton Veselov
- @ Anton Veselov Ok, then. It would be nice if you could add this to the question text. - artoodetoo
|
Timestamp range:
$at = strtotime('25-04-2016'); //1461535200 $to = strtotime('26-04-2016'); //1461621600 SELECT * FROM table WHERE timestamp >= $at AND timestamp < $to Date Range:
SELECT * FROM table WHERE DATE(timestamp) = '2016-05-20' SELECT * FROM table WHERE timestamp >= '2016-05-20 00:00:00' AND timestamp <= '2016-05-20 23:59:59' - Psb, and if so? WHERE YEAR (
date) = YEAR (DATE_ADD (NOW (), INTERVAL -1 YEAR)) - Anton Veselov
|
Solved the problem with the following SQL query
SELECT * FROM currency WHERE date >= DATE_SUB(CURRENT_DATE, INTERVAL 1 YEAR) AND chcode = ? GROUP BY date, chcode |
year()month()week()... - Sanek Zhitnik