In this query which I got help from #jarlh, in this question ORDER BY CASE
SELECT *
FROM booking
WHERE booking_date=$b_date OR DATE(delivery_date)=$b_date
ORDER BY CASE WHEN booking_date=$b_date
THEN hour * 60 + minute
WHEN DATE(delivery_date)=$b_date
THEN HOUR(delivery_date) * 60 + MINUTE(delivery_date)
END ASC
I need to find those posts with the same HOUR and MINUTE, before I leave the query, and mark them somehow, so I’m able to print them (those in the same hour and minute) horizontally and not vertically.
Related
I have a problem when I try get the total number of minutes in 2 rows. For example:
I don't know in mysql how to calculate this time with a query.
I want get minutes of 1 rows include time start and time end to use it.
your query...
SELECT ((HOUR('time end') * 60) + MINUTE('time end')) - ((HOUR('time start') * 60) + MINUTE('time start'))
In a table I have a column called 'service_time' which is the start date of a service, and another column 'times_year' which is how many times per year the service shall be done.
The problem I have is how I should select the rows when -> (thisMonth == service_time (month)) OR (thisMonth == service_time (month) + 12/times_year)
The tricky part that I cannot solve is how should I do this if the 'times_year' is a value between 1-4? Without making 4 different OR's??
So should it be something of a loop decided by the 'times_year' instead of doing a OR query to check all 4 OR's every single time even if the 'times_year' isn't 4?
UPDATE:
Well I need help how to do a loop in the sql query statement wich will only select if the 'month' is correct, so basiclly now its the '08' and if the service_time is set to 2015-03-01 (day doesnt matter) it shall do a check with the times_year to check if 2015-03-01 shall be having a service > 2015-03 + 12/4(as an example) then 2015-06, 2015-09, 2015-12, 2016-03 Then no dont select and so on...
I could do this in PHP but it will not be efficent in the longer term
Hopefully, you don't have times_year values like 5, 11, and so forth. That will get a little nasty to work out. Values like 1,2,3,4,6,12 are good.
You can determine the next service time after the start time like this in MySQL
service_time + INTERVAL (12 DIV times_year) MONTH
Similarly, the third service time after the start time will work like this.
service_time + INTERVAL 3*(12 DIV times_year) MONTH
Maybe your business rules call for service to be due on or before the last calendar day of each month. You can display that like so.
LAST_DAY(service_time + INTERVAL 3*(12 DIV times_year) MONTH)
Finally, you may wish to display the next service due date after today (after CURDATE()) How do you do that?
First, it has been this many months since the first service time:
TIMESTAMPDIFF(MONTH, service_time, CURDATE())
Next, you want to know how many service intervals that covers. Easy:
times_year * TIMESTAMPDIFF(MONTH, service_time, CURDATE()) / 12.0
Next, you want the next highest service interval number:
CEIL(times_year * TIMESTAMPDIFF(MONTH, CURDATE(), service_time) / 12.0)
Finally, plug that into the next-service-time function:
service_time + INTERVAL
CEIL(times_year * TIMESTAMPDIFF(MONTH, service_time, CURDATE()) / 12.0)*
(12 DIV times_year) MONTH
It's a long formula, but it works.
Finally, you can use this WHERE clause to find out all the services due before the end of the present month.
WHERE LAST_DAY(service_time + INTERVAL
CEIL(times_year * TIMESTAMPDIFF(MONTH, service_time, CURDATE()) / 12.0)*
(12 DIV times_year) MONTH) <= LAST_DAY(CURDATE())
I have a cron that runs some php with some mysql just after midnight everyday. I want to take all registered users (to my website) and send them a reminder and copy of the newsletter. However I want to do this every 30 days from their registration.
I have thought as far as this:
SELECT * FROM users
WHERE DATE(DT_stamp) = DATE(NOW() - INTERVAL 30 DAY
But this will only work for 30 days after they have registered, not 60 and 90.
Effectively I want:
Where days since registration is divisible by 30
That way every 30 days that user will get picked up in the sql.
Can someone help me formulate this WHERE clause, I am struggling with mysql where day(date1-date2) divisible 30
The DATEDIFF function returns the difference between two dates in days, ignoring the time:
SELECT * FROM users
WHERE DATEDIFF(DT_stamp, NOW()) % 30 = 0
or the other way round...
SELECT * FROM users WHERE MOD(DATEDIFF(NOW(),registration_date),30) = 0;
Use SQL modulo function MOD():
SELECT * FROM users
WHERE MOD( DATE(DT_stamp) - DATE(NOW()), 30) = 0
In mysql, you can also use the % operator, which does the same thing:
SELECT * FROM users
WHERE (DATE(DT_stamp) - DATE(NOW()) % 30 = 0
Just an addition (not that nine years had passed :)
If you want to skip today's date you should add
AND DATEDIFF(NOW(), DT_stamp) != 0;
making it
SELECT * FROM users WHERE MOD(DATEDIFF(NOW(), DT_stamp), 30) = 0 AND DATEDIFF(NOW(), DT_stamp) != 0;
first of all sorry for that title, but I have no idea how to describe it:
I'm saving sessions in my table and I would like to get the count of sessions per hour to know how many sessions were active over the day. The sessions are specified by two timestamps: start and end.
Hopefully you can help me.
Here we go:
http://sqlfiddle.com/#!2/bfb62/2/0
While I'm still not sure how you'd like to compare the start and end dates, looks like using COUNT, YEAR, MONTH, DAY, and HOUR, you could come up with your desired results.
Possibly something similar to this:
SELECT COUNT(ID), YEAR(Start), HOUR(Start), DAY(Start), MONTH(Start)
FROM Sessions
GROUP BY YEAR(Start), HOUR(Start), DAY(Start), MONTH(Start)
And the SQL Fiddle.
What you want to do is rather hard in MySQL. You can, however, get an approximation without too much difficulty. The following counts up users who start and stop within one day:
select date(start), hour,
sum(case when hours.hour between hour(start) and hours.hour then 1 else 0
end) as GoodEstimate
from sessions s cross join
(select 0 as hour union all
select 1 union all
. . .
select 23
) hours
group by date(start), hour
When a user spans multiple days, the query is harder. Here is one approach, that assumes that there exists a user who starts during every hour:
select thehour, count(*)
from (select distinct date(start), hour(start),
(cast(date(start) as datetime) + interval hour(start) hour as thehour
from sessions
) dh left outer join
sessions s
on s.start <= thehour + interval 1 hour and
s.end >= thehour
group by thehour
Note: these are untested so might have syntax errors.
OK, this is another problem where the index table comes to the rescue.
An index table is something that everyone should have in their toolkit, preferably in the master database. It is a table with a single id int primary key indexed column containing sequential numbers from 0 to n where n is a number big enough to do what you need, 100,000 is good, 1,000,000 is better. You only need to create this table once but once you do you will find it has all kinds of applications.
For your problem you need to consider each hour and, if I understand your problem you need to count every session that started before the end of the hour and hasn't ended before that hour starts.
Here is the SQL fiddle for the solution.
What it does is use a known sequential number from the indextable (only 0 to 100 for this fiddle - just over 4 days - you can see why you need a big n) to link with your data at the top and bottom of the hour.
I'm making a fitness logbook where indoor rowers can log there results.
To make it interesting and motivating I'm implementing an achievement system.
I like to have an achievement that if someone rows more than 90 times within 24 weeks they get that achievement.
Does anybody have some hints in how i can implement this in MYSQL.
The mysql-table for the logbook is pretty straightforward: id, userid, date (timestamp),etc (rest is omitted because it doesn't really matter)
The jist is that the first rowdate and the last one can't exceed the 24 weeks.
I assume from your application that you want the most recent 24 weeks.
In mysql, you do this as:
select lb.userid
from logbook lb
where datediff(now(), lb.date) >= 7*24
group by userid
having count(*) >= 90
If you need it for an arbitrary 24-week period, can you modify the question?
Just do a sql query to count the number of rows a user has between now and 24 weeks ago. This is a pretty straight forward query to run.
Look at using something with datediff in mysql to get the difference between now and 24 weeks ago.
After you have a script set up to do this, set up a cron job to run either every day or every week and do some automation on this.
I think you should create a table achievers which you populate with the achievers of each day.
You can set a recurrent(daily, right before midnight) event in which you run a query like this:
delete from achievers;
insert into achievers (
select userid
from logbook
where date < currenttimestamp and date > currenttimestamp - 24weeks
group by userid
having count(*) >= 90
)
For events in mysql: http://dev.mysql.com/doc/refman/5.1/en/events-overview.html
This query will give you the list of users total activity in 24 weeks
select * from table groupby userid where `date` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 168 DAY ) AND CURDATE( ) having count(id) >= 90