I have a mysql MyIsam table with a field datestamp (datetime), I am trying to return all records that have a datestamp within the last 3 hours, but group it into 10 chunks (so for this example group one would be the first 18 minutes etc...)
Any ideas? Is this possible in mysql?
I think this may work:
$t = date("U",strtotime("-3 Hours"));
$sql = "SELECT *, FLOOR((UNIX_TIMESTAMP(datestamp) - ".$t.") / 1080) as chunk
FROM table WHERE UNIX_TIMESTAMP(datestamp) > '".$t."' GROUP BY chunk";
Devide the difference with 1/10th of the 3 hour interval (1080 sec). This gives chunks 0,1,2...9, where 0 means the first 18 minutes.
Sure. As I'm not really a MySQL expert, this is pseudocode:
SELECT ..., (now() - datestamp) AS age, age / 600 AS group, ...
Then you have two columns, "age" that you use to limit the selection, and "group" that further subdivides.
use like
SELECT SUM(myfields)..... FROM mytable
WHERE mydate >= DATE_SUB( NOW() , INTERVAL 3 HOUR)
GROUP BY FLOOR((UNIX_TIMESTAMP() - UNIX_TIMESTAMP(mydate) ) / 10800)
10800 is the number of second in 3 hours
Related
I want to get data from training_course table where current date minus 5 days is equal to training_end_date. Training_end_date is my field in the table.
Thanks
You seem to want:
select *
from training_course
where training_end_date = current_date - interval 5 day
Or, if your dates have time components, you maybe want:
select *
from training_course
where training_end_date >= current_date - interval 5 day and training_date < current_date - interval 4 day
I have a table in database which has the following columns
id, timestamp,user_id
I want to pass two dates and it should find the sum of the records. for example i pass from = 01-12-2014 and 15-12-2014 and it should show a record like
00-04 5
04-08 40
08-12 30
12-16 20
16-20 32
20-24 50
you should group by date and count or sum after your records column something like : "select sum(records) from db_table group by added_date"
For every distinct date he will add/count ( depends on what you want ) the records.
i used a loop to increase time by 14400 and the query is following
select count(id) as sessions from user_history where date_format(login_timestamp, '%d-%m-%Y') < date_format(FROM_UNIXTIME(1157065200), '%d-%m-%Y') and date(login_timestamp) > date_format(FROM_UNIXTIME(1157079600), '%d-%m-%Y') and date_format(login_timestamp, '%H:%i:%s') <= date_format(FROM_UNIXTIME(1157065200), '%H:%i:%s')
I have a fun one for you. I have a database with the date columns free_from and free_until. What I need to find is the amount of days between now and 1 month today which are free. For example, if the current date was 2013/01/15 and the columns were as follows:
free_from | free_until
2013/01/12| 2013/01/17
2013/01/22| 2013/01/26
2013/01/29| 2013/02/04
2013/02/09| 2013/02/11
2013/02/14| 2013/02/17
2013/02/19| 2013/02/30
The answer would be 16
as 2 + 4 + 6 + 2 + 2 + 0 = 16
The first row only starts counting at the 15th rather than the 12th
since the 15th is the current date.
The last row is discounted because none of the dates are within a
month of the current date.
The dates must be counted as it the free_from date is inclusive and
the free_until date is exclusive.
I'm assuming DATEDIFF() will be used somewhere along the line, but I can't, for the life of me, work this one out.
Thanks for your time!
Edit: This is going into PHP mysql_query so that might restrict you a little concerning what you can do with MYSQL.
SET #today = "2013-01-15";
SET #nextm = DATE_ADD(#today, INTERVAL 1 month);
SET #lastd = DATE_ADD(#nextm, INTERVAL 1 day);
SELECT
DATEDIFF(
IF(#lastd> free_until, free_until, #lastd),
IF(#today > free_from, #today, free_from)
)
FROM `test`
WHERE free_until >= #today AND free_from < #nextm
That should work. At least for your test data. But what day is 2013/02/30? :-)
Dont forget to change #today = CURDATE();
The best I can think of is something like:
WHERE free_until > CURDATE()
AND free_from < CURDATE() + INTERVAL '1' MONTH
That will get rid of any unnecessary rows. Then on the first row do in PHP:
date_diff(date(), free_until)
On the last row, do:
date_diff(free_from, strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month"))
Then on intermediate dates do:
date_diff(free_from, free_until)
Something to that effect, but this seems extremely clunky and convoluted...
From the top of my mind... first do a:
SELECT a.free_from AS a_from, a.free_until AS a_until, b.free_from AS b_from
FROM availability a
INNER JOIN availability b ON b.free_from > a.free_until
ORDER BY a_from, b_from
This probably will return a set of rows where for each row interval you have next i.e. greater intervals. The results are ordered strategically. You can then wrap the results in a partial group by:
SELECT * FROM (
SELECT a.free_from AS a_from, a.free_until AS a_until, b.free_from AS b_from
FROM availability a
INNER JOIN availability b ON b.free_from > a.free_until
ORDER BY a_from, b_from
) AS NextInterval
GROUP BY a_from, b_until
In the above query, add a DATE_DIFF clause (wrap it in SUM() if necessary):
DATE_DIFF(b_until, a_from)
I will like to understand the most efficient way to achieve this:
I have a MySQL table with my users info, including birthday (YYYY/MM/DD). My objective is to retrieve an array (php) with the total of user for each individual age from 10 to 60 years old. I can just query all my users, pass the birthday to a birthday to age php function I wrote and then populate an array with the totals. But I wonder if there's a way to build an sql query that does this job for me?
You should be able to group the rows:
SELECT
FLOOR(DATEDIFF(NOW(), `birthday`)/365) AS 'Age',
COUNT(*)
FROM `users`
WHERE FLOOR(DATEDIFF(NOW(), `birthday`)/365) BETWEEN 10 AND 60
AND `id` IN (1, 3, 5, 12, 29)
GROUP BY FLOOR(DATEDIFF(NOW(), `birthday`)/365)
ORDER BY FLOOR(DATEDIFF(NOW(), `birthday`)/365) ASC
The result won't contain ages that have no users, but I'm not sure why you'd need that.
Updates
- Added an id filter
- Fixed the date calculation (oops!)
- Named the new column
SELECT * FROM tableName
WHERE dateOfBirth >= (CURRENT_DATE - INTERVAL 60 YEAR)
AND dateOfBirth <= ( CURRENT_DATE - INTERVAL 10 YEAR )
Hopefully I don't get slammed by the database experts on this one...
You will need to use MySql's DATEDIFF().
SELECT USER, DATEOFBIRTH FROM USERTABLE WHERE DATEDIFF(DATEOFBIRTH,NOW()) < 60;
How i can select from database the items older than 12 hours?!
I using a timestamp column to store the time but I don't think i need year,month,day only hours
I have something like this but it dosen't work (no error just returning all data from table)
$sql = "SELECT *FROM Y WHERE X and time > now() - INTERVAL 12 HOUR";
Type : timestamp
You're right Salman A .Thanks
Try this :
SELECT * FROM Y WHERE X and time < (NOW() - INTERVAL 12 HOUR)
you need < rather than > as you want to select records older than 12 hours